package com.chart.web.controller.chart;

import com.chart.chart.domian.ChartLegendData;
import com.chart.chart.domian.dto.ChartDateTto;
import com.chart.chart.domian.utils.ChartValidator;
import com.chart.chart.service.impl.ChartConfigsUserServiceImpl;
import com.chart.common.utils.ColorUtils;
import com.chart.common.utils.ExcelDataUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.TextNode;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.chart.chart.domian.ChartConfigs;
import com.chart.chart.domian.vo.ChartSmartVo;
import com.chart.chart.service.IChartConfigsService;
import com.chart.common.core.domain.AjaxResult;
import com.chart.common.utils.ChartDataUtils;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

/**
 * 根据j数据 和颜色效果hi
 *
 * @author ljq
 * 2024/7/10 9:44
 */
@Slf4j
@RestController
@RequestMapping("/smart/chart")
public class ChartOperationsController {

    @Autowired
    private IChartConfigsService chartConfigsService;
    @Autowired
    private ChartConfigsUserServiceImpl chartConfigsUserService;


    /**
     * 心电图
     * 第一组：[第一列，第二列]
     * <p>
     * <p>
     * 第二组：[第一列，第三列]
     * <p>
     * <p>
     * 第三组：[第一列，第四列]
     *
     * @param
     * @return
     * @throws Exception 心电图
     */


    // 计算平均值
    private static double calculateMean(List<Double> list) {
        double sum = 0;
        for (Double value : list) {
            sum += value;
        }
        return sum / list.size();
    }

    // 计算标准差
    private static double calculateStandardDeviation(List<Double> list, double mean) {
        double sum = 0;
        for (Double value : list) {
            sum += Math.pow(value - mean, 2);
        }
        return Math.sqrt(sum / (list.size() - 1)); // 样本标准差
    }


    @ApiOperation("误差棒")
    @PostMapping("/importData8")
    @Transactional(rollbackFor = Exception.class) // 添加事务管理注解，捕获所有异常回滚事务
    public AjaxResult importData8(@RequestBody ChartDateTto chartDateTto) throws Exception {
        // 参数校验
        String validationMessage = ChartValidator.check(chartDateTto);
        if (validationMessage != null) {
            // 返回校验失败的提示信息给前端
            return AjaxResult.error(validationMessage);
        }

        try {
            Integer errorBarType = chartDateTto.getErrorBarType();
            if (chartDateTto.getConfigId() == 61 || chartDateTto.getConfigId() == 62) {
                if (errorBarType == 0) {
                    //标准差

                    // 提取数据
                    List<List<String>> getdata = extractDataFromListNullString(chartDateTto.getDataList());
                    List<String> lenNameList = getdata.get(0);
                    lenNameList.remove(0);
                    String chartValue = chartDateTto.getChartValue();

                    // 删除第一行的数据
                    getdata.remove(0);
                    // 删除每行的第一列数据
                    for (List<String> getdatum : getdata) {
                        getdatum.remove(0);
                    }

                    // 按列分组
                    int columnCount = getdata.get(0).size();
                    List<List<Double>> groupedData = new ArrayList<>();

                    for (int i = 0; i < columnCount; i++) {
                        List<Double> columnData = new ArrayList<>();
                        for (List<String> row : getdata) {
                            columnData.add(Double.parseDouble(row.get(i)));
                        }
                        groupedData.add(columnData);
                    }

                    // 打印每列数据
                    for (List<Double> groupedDatum : groupedData) {
                        System.out.println("我是打印的每一列" + groupedDatum);
                    }
                    // 计算每组数据的平均值和标准差
                    ArrayList<Double> averages = new ArrayList<>();
                    ArrayList<Double> stdDevs = new ArrayList<>();
                    for (List<Double> groupedDatum : groupedData) {
                        double sum = groupedDatum.stream().mapToDouble(Double::doubleValue).sum();
                        double average = sum / groupedDatum.size();
                        averages.add(average);

                        // 计算标准差
                        double stdDev = calculateStandardDeviation(groupedDatum, average);
                        stdDevs.add(stdDev);
                    }

                    // 打印平均值
                    for (Double avg : averages) {
                        System.out.println("我是平均值" + avg);
                    }

                    // 打印每组名称
                    for (String s : lenNameList) {
                        System.out.println("我是每组名称" + s);
                    }

                    // 将每个平均值和对应的组名称进行配对并放入 group 集合中
                    ArrayList<ArrayList<Object>> group = new ArrayList<>();
                    for (int i = 0; i < averages.size(); i++) {
                        String name = lenNameList.get(i);
                        Double average = averages.get(i);
                        ArrayList<Object> pairedData = new ArrayList<>();
                        pairedData.add(name);
                        pairedData.add(average);
                        group.add(pairedData);
                    }

                    // 打印分组后的数据
                    for (Object obj : group) {
                        System.out.println("我是分组数据: " + obj);
                    }

                    // 计算每组数据的上限和下限（使用标准差）
                    ArrayList<Double> upperLimits = new ArrayList<>();
                    ArrayList<Double> lowerLimits = new ArrayList<>();
                    for (int i = 0; i < groupedData.size(); i++) {
                        double average = averages.get(i);
                        double stdDev = stdDevs.get(i);
                        double newUpper = average + stdDev; // 使用标准差
                        double newLower = average - stdDev; // 使用标准差
                        upperLimits.add(newUpper);
                        lowerLimits.add(newLower);
                    }

                    // 打印上限和下限
                    for (int i = 0; i < upperLimits.size(); i++) {
                        System.out.println("我是" + lenNameList.get(i) + "的上限: " + upperLimits.get(i) + ", 下限: " + lowerLimits.get(i));
                    }

                    // 将上限和下限以数组的形式放入集合
                    ArrayList<ArrayList<Object>> limitsGroup = new ArrayList<>();
                    for (int i = 0; i < upperLimits.size(); i++) {
                        String name = lenNameList.get(i);
                        double newUpper = upperLimits.get(i);
                        double newLower = lowerLimits.get(i);

                        ArrayList<Object> limitsData = new ArrayList<>();
                        limitsData.add(name);      // 添加组名称
                        limitsData.add(newUpper);  // 添加新的上限
                        limitsData.add(newLower);  // 添加新的下限
                        limitsGroup.add(limitsData);
                    }

                    // 打印新的上限和下限
                    ArrayList<ArrayNode> formattedLimitsList = new ArrayList<>();
                    ObjectMapper objectMapper = new ObjectMapper();

                    for (ArrayList<Object> limitsData : limitsGroup) {
                        String name = (String) limitsData.get(0);
                        Double newUpperLimit = (Double) limitsData.get(1);
                        Double newLowerLimit = (Double) limitsData.get(2);

                        // 创建一个 JSON 数组节点
                        ArrayNode limitArrayNode = objectMapper.createArrayNode();
                        limitArrayNode.add(name);
                        limitArrayNode.add(newUpperLimit);
                        limitArrayNode.add(newLowerLimit);

                        // 打印格式化的结果
                        System.out.println(limitArrayNode.toString());
                        // 将格式化的数组添加到新集合中
                        formattedLimitsList.add(limitArrayNode);
                    }

                    // 解析 chartValue
                    JsonNode rootNode = objectMapper.readTree(chartValue);

                    // 获取 series 数组并清空第二个对象的 data 数组
                    JsonNode seriesNode = rootNode.path("series");
                    if (seriesNode.isArray() && seriesNode.size() > 1) {
                        JsonNode secondSeriesNode = seriesNode.get(1);
                        JsonNode dataNode = secondSeriesNode.path("data");
                        if (dataNode.isArray()) {
                            // 清空 data 数组
                            ((ArrayNode) dataNode).removeAll(); // 删除所有元素

                            // 将 formattedLimitsList 的内容添加到 data 数组中
                            for (ArrayNode limit : formattedLimitsList) {
                                ((ArrayNode) dataNode).add(limit);
                            }
                        }
                    }

                    // 获取 dataset 数组并清空内容
                    JsonNode datasetNode = rootNode.path("dataset");
                    if (datasetNode.isArray() && datasetNode.size() > 0) {
                        JsonNode sourceNode = datasetNode.get(0).path("source");
                        if (sourceNode.isArray()) {
                            // 清空 source 数组
                            ((ArrayNode) sourceNode).removeAll();

                            // 将 group 的内容替换到 source 数组中
                            for (Object obj : group) {
                                ((ArrayNode) sourceNode).add(objectMapper.valueToTree(obj));
                            }
                        }
                    }

                    // 将修改后的 JSON 转换为字符串并返回
                    String updatedChartValue = objectMapper.writeValueAsString(rootNode);
                    ArrayList<ChartSmartVo> newList = new ArrayList<>();
                    ChartSmartVo chartSmartVo = new ChartSmartVo();
                    chartSmartVo.setConfigValue(updatedChartValue);
                    chartSmartVo.setUniList(lenNameList);
                    newList.add(chartSmartVo);

                    return AjaxResult.success(newList);


                } else if (errorBarType == 1) {
                    //标准误
                    List<List<String>> getdata = extractDataFromListNullString(chartDateTto.getDataList());
                    List<String> lenNameList = getdata.get(0);
                    lenNameList.remove(0);
                    String chartValue = chartDateTto.getChartValue();

                    // 删除第一行的数据
                    getdata.remove(0);
                    // 删除每行的第一列数据
                    for (List<String> getdatum : getdata) {
                        getdatum.remove(0);
                    }

                    // 按列分组
                    int columnCount = getdata.get(0).size();
                    List<List<Double>> groupedData = new ArrayList<>();

                    for (int i = 0; i < columnCount; i++) {
                        List<Double> columnData = new ArrayList<>();
                        for (List<String> row : getdata) {
                            columnData.add(Double.parseDouble(row.get(i)));
                        }
                        groupedData.add(columnData);
                    }

                    // 打印每列数据
                    for (List<Double> groupedDatum : groupedData) {
                        System.out.println("我是打印的每一列" + groupedDatum);
                    }

                    // 计算每组数据的平均值和标准差
                    ArrayList<Double> averages = new ArrayList<>();
                    ArrayList<Double> stdDevs = new ArrayList<>();
                    for (List<Double> groupedDatum : groupedData) {
                        double sum = groupedDatum.stream().mapToDouble(Double::doubleValue).sum();
                        double average = sum / groupedDatum.size();
                        averages.add(average);

                        // 计算标准差
                        double stdDev = calculateStandardDeviation(groupedDatum, average);
                        stdDevs.add(stdDev);
                    }

                    // 打印平均值
                    for (Double avg : averages) {
                        System.out.println("我是平均值" + avg);
                    }

                    // 打印每组名称
                    for (String s : lenNameList) {
                        System.out.println("我是每组名称" + s);
                    }

                    // 将每个平均值和对应的组名称进行配对并放入 group 集合中
                    ArrayList<ArrayList<Object>> group = new ArrayList<>();
                    for (int i = 0; i < averages.size(); i++) {
                        String name = lenNameList.get(i);
                        Double average = averages.get(i);
                        ArrayList<Object> pairedData = new ArrayList<>();
                        pairedData.add(name);
                        pairedData.add(average);
                        group.add(pairedData);
                    }

                    // 打印分组后的数据
                    for (Object obj : group) {
                        System.out.println("我是分组数据: " + obj);
                    }

                    // 计算每组数据的上限和下限（使用标准误）
                    ArrayList<Double> upperLimits = new ArrayList<>();
                    ArrayList<Double> lowerLimits = new ArrayList<>();
                    for (int i = 0; i < groupedData.size(); i++) {
                        double average = averages.get(i);
                        double stdDev = stdDevs.get(i);
                        double n = groupedData.get(i).size(); // 样本大小

                        // 计算标准误
                        double standardError = stdDev / Math.sqrt(n);

                        // 计算误差棒的上限和下限
                        double newUpper = average + standardError; // 使用标准误
                        double newLower = average - standardError; // 使用标准误
                        upperLimits.add(newUpper);
                        lowerLimits.add(newLower);
                    }

                    // 打印上限和下限
                    for (int i = 0; i < upperLimits.size(); i++) {
                        System.out.println("我是" + lenNameList.get(i) + "的上限: " + upperLimits.get(i) + ", 下限: " + lowerLimits.get(i));
                    }

                    // 将上限和下限以数组的形式放入集合
                    ArrayList<ArrayList<Object>> limitsGroup = new ArrayList<>();
                    for (int i = 0; i < upperLimits.size(); i++) {
                        String name = lenNameList.get(i);
                        double newUpper = upperLimits.get(i);
                        double newLower = lowerLimits.get(i);

                        ArrayList<Object> limitsData = new ArrayList<>();
                        limitsData.add(name);      // 添加组名称
                        limitsData.add(newUpper);  // 添加新的上限
                        limitsData.add(newLower);  // 添加新的下限
                        limitsGroup.add(limitsData);
                    }

                    // 打印新的上限和下限
                    ArrayList<ArrayNode> formattedLimitsList = new ArrayList<>();
                    ObjectMapper objectMapper = new ObjectMapper();

                    for (ArrayList<Object> limitsData : limitsGroup) {
                        String name = (String) limitsData.get(0);
                        Double newUpperLimit = (Double) limitsData.get(1);
                        Double newLowerLimit = (Double) limitsData.get(2);

                        // 创建一个 JSON 数组节点
                        ArrayNode limitArrayNode = objectMapper.createArrayNode();
                        limitArrayNode.add(name);
                        limitArrayNode.add(newUpperLimit);
                        limitArrayNode.add(newLowerLimit);

                        // 打印格式化的结果
                        System.out.println(limitArrayNode.toString());
                        // 将格式化的数组添加到新集合中
                        formattedLimitsList.add(limitArrayNode);
                    }

                    // 解析 chartValue
                    JsonNode rootNode = objectMapper.readTree(chartValue);

                    // 获取 series 数组并清空第二个对象的 data 数组
                    JsonNode seriesNode = rootNode.path("series");
                    if (seriesNode.isArray() && seriesNode.size() > 1) {
                        JsonNode secondSeriesNode = seriesNode.get(1);
                        JsonNode dataNode = secondSeriesNode.path("data");
                        if (dataNode.isArray()) {
                            // 清空 data 数组
                            ((ArrayNode) dataNode).removeAll(); // 删除所有元素

                            // 将 formattedLimitsList 的内容添加到 data 数组中
                            for (ArrayNode limit : formattedLimitsList) {
                                ((ArrayNode) dataNode).add(limit);
                            }
                        }
                    }
                    // 获取 dataset 数组并清空内容
                    JsonNode datasetNode = rootNode.path("dataset");
                    if (datasetNode.isArray() && datasetNode.size() > 0) {
                        JsonNode sourceNode = datasetNode.get(0).path("source");
                        if (sourceNode.isArray()) {
                            // 清空 source 数组
                            ((ArrayNode) sourceNode).removeAll();

                            // 将 group 的内容替换到 source 数组中
                            for (Object obj : group) {
                                ((ArrayNode) sourceNode).add(objectMapper.valueToTree(obj));
                            }
                        }
                    }

                    // 将修改后的 JSON 转换为字符串并返回
                    String updatedChartValue = objectMapper.writeValueAsString(rootNode);
                    ArrayList<ChartSmartVo> newList = new ArrayList<>();
                    ChartSmartVo chartSmartVo = new ChartSmartVo();
                    chartSmartVo.setConfigValue(updatedChartValue);
                    chartSmartVo.setUniList(lenNameList);
                    newList.add(chartSmartVo);

                    return AjaxResult.success(newList);

                } else if (errorBarType == 2) { //置信度
                    // 提取数据
                    List<List<String>> getdata = extractDataFromListNullString(chartDateTto.getDataList());
                    List<String> lenNameList = getdata.get(0);
                    lenNameList.remove(0);
                    String chartValue = chartDateTto.getChartValue();

                    // 删除第一行的数据
                    getdata.remove(0);
                    // 删除每行的第一列数据
                    for (List<String> getdatum : getdata) {
                        getdatum.remove(0);
                    }

                    // 按列分组
                    int columnCount = getdata.get(0).size();
                    List<List<Double>> groupedData = new ArrayList<>();

                    for (int i = 0; i < columnCount; i++) {
                        List<Double> columnData = new ArrayList<>();
                        for (List<String> row : getdata) {
                            columnData.add(Double.parseDouble(row.get(i)));
                        }
                        groupedData.add(columnData);
                    }

                    // 打印每列数据
                    for (List<Double> groupedDatum : groupedData) {
                        System.out.println("我是打印的每一列" + groupedDatum);
                    }

                    // 计算每组数据的平均值和标准差
                    ArrayList<Double> averages = new ArrayList<>();
                    ArrayList<Double> stdDevs = new ArrayList<>();
                    for (List<Double> groupedDatum : groupedData) {
                        double sum = groupedDatum.stream().mapToDouble(Double::doubleValue).sum();
                        double average = sum / groupedDatum.size();
                        averages.add(average);

                        // 计算标准差
                        double stdDev = calculateStandardDeviation(groupedDatum, average);
                        stdDevs.add(stdDev);
                    }

                    // 打印平均值
                    for (Double avg : averages) {
                        System.out.println("我是平均值" + avg);
                    }

                    // 打印每组名称
                    for (String s : lenNameList) {
                        System.out.println("我是每组名称" + s);
                    }

                    // 将每个平均值和对应的组名称进行配对并放入 group 集合中
                    ArrayList<ArrayList<Object>> group = new ArrayList<>();
                    for (int i = 0; i < averages.size(); i++) {
                        String name = lenNameList.get(i);
                        Double average = averages.get(i);
                        ArrayList<Object> pairedData = new ArrayList<>();
                        pairedData.add(name);
                        pairedData.add(average);
                        group.add(pairedData);
                    }

                    // 打印分组后的数据
                    for (Object obj : group) {
                        System.out.println("我是分组数据: " + obj);
                    }

                    // 计算每组数据的上限和下限（使用置信区间）
                    ArrayList<Double> upperLimits = new ArrayList<>();
                    ArrayList<Double> lowerLimits = new ArrayList<>();
                    double zValue = 1.96; // 95%置信水平的Z值
                    for (int i = 0; i < groupedData.size(); i++) {
                        double average = averages.get(i);
                        double stdDev = stdDevs.get(i);
                        double n = groupedData.get(i).size(); // 样本大小

                        // 计算置信区间的上限和下限
                        double marginOfError = zValue * (stdDev / Math.sqrt(n));
                        double newUpper = average + marginOfError; // 使用置信区间
                        double newLower = average - marginOfError; // 使用置信区间
                        upperLimits.add(newUpper);
                        lowerLimits.add(newLower);
                    }

                    // 打印上限和下限
                    for (int i = 0; i < upperLimits.size(); i++) {
                        System.out.println("我是" + lenNameList.get(i) + "的上限: " + upperLimits.get(i) + ", 下限: " + lowerLimits.get(i));
                    }

                    // 将上限和下限以数组的形式放入集合
                    ArrayList<ArrayList<Object>> limitsGroup = new ArrayList<>();
                    for (int i = 0; i < upperLimits.size(); i++) {
                        String name = lenNameList.get(i);
                        double newUpper = upperLimits.get(i);
                        double newLower = lowerLimits.get(i);

                        ArrayList<Object> limitsData = new ArrayList<>();
                        limitsData.add(name);      // 添加组名称
                        limitsData.add(newUpper);  // 添加新的上限
                        limitsData.add(newLower);  // 添加新的下限
                        limitsGroup.add(limitsData);
                    }
                    // 打印新的上限和下限
                    ArrayList<ArrayNode> formattedLimitsList = new ArrayList<>();
                    ObjectMapper objectMapper = new ObjectMapper();

                    for (ArrayList<Object> limitsData : limitsGroup) {
                        String name = (String) limitsData.get(0);
                        Double newUpperLimit = (Double) limitsData.get(1);
                        Double newLowerLimit = (Double) limitsData.get(2);

                        // 创建一个 JSON 数组节点
                        ArrayNode limitArrayNode = objectMapper.createArrayNode();
                        limitArrayNode.add(name);
                        limitArrayNode.add(newUpperLimit);
                        limitArrayNode.add(newLowerLimit);

                        // 打印格式化的结果
                        System.out.println(limitArrayNode.toString());
                        // 将格式化的数组添加到新集合中
                        formattedLimitsList.add(limitArrayNode);
                    }

                    // 解析 chartValue
                    JsonNode rootNode = objectMapper.readTree(chartValue);
                    // 获取 series 数组并清空第二个对象的 data 数组
                    JsonNode seriesNode = rootNode.path("series");
                    if (seriesNode.isArray() && seriesNode.size() > 1) {
                        JsonNode secondSeriesNode = seriesNode.get(1);
                        JsonNode dataNode = secondSeriesNode.path("data");
                        if (dataNode.isArray()) {
                            // 清空 data 数组
                            ((ArrayNode) dataNode).removeAll(); // 删除所有元素
                            // 将 formattedLimitsList 的内容添加到 data 数组中
                            for (ArrayNode limit : formattedLimitsList) {
                                ((ArrayNode) dataNode).add(limit);
                            }
                        }
                    }
                    // 获取 dataset 数组并清空内容
                    JsonNode datasetNode = rootNode.path("dataset");
                    if (datasetNode.isArray() && datasetNode.size() > 0) {
                        JsonNode sourceNode = datasetNode.get(0).path("source");
                        if (sourceNode.isArray()) {
                            // 清空 source 数组
                            ((ArrayNode) sourceNode).removeAll();

                            // 将 group 的内容替换到 source 数组中
                            for (Object obj : group) {
                                ((ArrayNode) sourceNode).add(objectMapper.valueToTree(obj));
                            }
                        }
                    }

                    // 将修改后的 JSON 转换为字符串并返回
                    String updatedChartValue = objectMapper.writeValueAsString(rootNode);
                    ArrayList<ChartSmartVo> newList = new ArrayList<>();
                    ChartSmartVo chartSmartVo = new ChartSmartVo();
                    chartSmartVo.setConfigValue(updatedChartValue);
                    chartSmartVo.setUniList(lenNameList);
                    newList.add(chartSmartVo);

                    return AjaxResult.success(newList);

                }


            }
        } catch (JsonProcessingException e) {
            log.error("误差棒,importData8,configId={}发生异常", chartDateTto.getConfigId());
            throw new RuntimeException(e);
        } catch (IllegalArgumentException e) {
            throw new RuntimeException(e);
        }

        return null;
    }


    @ApiOperation("基础柱状图，基础折线图")
    @PostMapping("/importData7")
    @Transactional(rollbackFor = Exception.class) // 添加事务管理注解，捕获所有异常回滚事务
    public AjaxResult importData7(@RequestBody ChartDateTto chartDateTto) throws Exception {

        // 参数校验
        String validationMessage = ChartValidator.check(chartDateTto);

        if (validationMessage != null) {
            // 返回校验失败的提示信息给前端
            return AjaxResult.error(validationMessage);
        }

        try {
            if (chartDateTto.getChartValue() != null) {
                Integer errorBarType = chartDateTto.getErrorBarType();
                if (chartDateTto.getConfigId() == 61 || chartDateTto.getConfigId() == 62) {
                    if (errorBarType == 0) {
                        //标准差
                        // 提取数据
                        List<List<String>> getdata = extractDataFromListNullString(chartDateTto.getDataList());
                        List<String> lenNameList = getdata.get(0);
                        lenNameList.remove(0);
                        String chartValue = chartDateTto.getChartValue();

                        // 删除第一行的数据
                        getdata.remove(0);
                        // 删除每行的第一列数据
                        for (List<String> getdatum : getdata) {
                            getdatum.remove(0);
                        }

                        // 按列分组
                        int columnCount = getdata.get(0).size();
                        List<List<Double>> groupedData = new ArrayList<>();

                        for (int i = 0; i < columnCount; i++) {
                            List<Double> columnData = new ArrayList<>();
                            for (List<String> row : getdata) {
                                columnData.add(Double.parseDouble(row.get(i)));
                            }
                            groupedData.add(columnData);
                        }

                        // 计算每组数据的平均值和标准差
                        ArrayList<Double> averages = new ArrayList<>();
                        ArrayList<Double> stdDevs = new ArrayList<>();
                        for (List<Double> groupedDatum : groupedData) {
                            double sum = groupedDatum.stream().mapToDouble(Double::doubleValue).sum();
                            double average = sum / groupedDatum.size();
                            averages.add(average);
                            // 计算标准差
                            double stdDev = calculateStandardDeviation(groupedDatum, average);
                            stdDevs.add(stdDev);
                        }

                        // 将每个平均值和对应的组名称进行配对并放入 group 集合中
                        ArrayList<ArrayList<Object>> group = new ArrayList<>();
                        for (int i = 0; i < averages.size(); i++) {
                            String name = lenNameList.get(i);
                            Double average = averages.get(i);
                            ArrayList<Object> pairedData = new ArrayList<>();
                            pairedData.add(name);
                            pairedData.add(average);
                            group.add(pairedData);
                        }

                        // 计算每组数据的上限和下限（使用标准差）
                        ArrayList<Double> upperLimits = new ArrayList<>();
                        ArrayList<Double> lowerLimits = new ArrayList<>();
                        for (int i = 0; i < groupedData.size(); i++) {
                            double average = averages.get(i);
                            double stdDev = stdDevs.get(i);
                            double newUpper = average + stdDev; // 使用标准差
                            double newLower = average - stdDev; // 使用标准差
                            upperLimits.add(newUpper);
                            lowerLimits.add(newLower);
                        }

                        // 将上限和下限以数组的形式放入集合
                        ArrayList<ArrayList<Object>> limitsGroup = new ArrayList<>();
                        for (int i = 0; i < upperLimits.size(); i++) {
                            String name = lenNameList.get(i);
                            double newUpper = upperLimits.get(i);
                            double newLower = lowerLimits.get(i);

                            ArrayList<Object> limitsData = new ArrayList<>();
                            limitsData.add(name);      // 添加组名称
                            limitsData.add(newUpper);  // 添加新的上限
                            limitsData.add(newLower);  // 添加新的下限
                            limitsGroup.add(limitsData);
                        }

                        // 打印新的上限和下限
                        ArrayList<ArrayNode> formattedLimitsList = new ArrayList<>();
                        ObjectMapper objectMapper = new ObjectMapper();

                        for (ArrayList<Object> limitsData : limitsGroup) {
                            String name = (String) limitsData.get(0);
                            Double newUpperLimit = (Double) limitsData.get(1);
                            Double newLowerLimit = (Double) limitsData.get(2);

                            // 创建一个 JSON 数组节点
                            ArrayNode limitArrayNode = objectMapper.createArrayNode();
                            limitArrayNode.add(name);
                            limitArrayNode.add(newUpperLimit);
                            limitArrayNode.add(newLowerLimit);

                            // 打印格式化的结果
                            System.out.println(limitArrayNode.toString());
                            // 将格式化的数组添加到新集合中
                            formattedLimitsList.add(limitArrayNode);
                        }

                        // 解析 chartValue
                        JsonNode rootNode = objectMapper.readTree(chartValue);

                        // 获取 series 数组并清空第二个对象的 data 数组
                        JsonNode seriesNode = rootNode.path("series");
                        if (seriesNode.isArray() && seriesNode.size() > 1) {
                            JsonNode secondSeriesNode = seriesNode.get(1);
                            JsonNode dataNode = secondSeriesNode.path("data");
                            if (dataNode.isArray()) {
                                // 清空 data 数组
                                ((ArrayNode) dataNode).removeAll(); // 删除所有元素

                                // 将 formattedLimitsList 的内容添加到 data 数组中
                                for (ArrayNode limit : formattedLimitsList) {
                                    ((ArrayNode) dataNode).add(limit);
                                }
                            }
                        }

                        // 获取 dataset 数组并清空内容
                        JsonNode datasetNode = rootNode.path("dataset");
                        if (datasetNode.isArray() && datasetNode.size() > 0) {
                            JsonNode sourceNode = datasetNode.get(0).path("source");
                            if (sourceNode.isArray()) {
                                // 清空 source 数组
                                ((ArrayNode) sourceNode).removeAll();

                                // 将 group 的内容替换到 source 数组中
                                for (Object obj : group) {
                                    ((ArrayNode) sourceNode).add(objectMapper.valueToTree(obj));
                                }
                            }
                        }

                        // 将修改后的 JSON 转换为字符串并返回
                        String updatedChartValue = objectMapper.writeValueAsString(rootNode);
                        ArrayList<ChartSmartVo> newList = new ArrayList<>();
                        ChartSmartVo chartSmartVo = new ChartSmartVo();
                        chartSmartVo.setConfigValue(updatedChartValue);
                        chartSmartVo.setUniList(lenNameList);
                        newList.add(chartSmartVo);
                        return AjaxResult.success(newList);

                    } else if (errorBarType == 1) {
                        //标准误
                        List<List<String>> getdata = extractDataFromListNullString(chartDateTto.getDataList());
                        List<String> lenNameList = getdata.get(0);
                        lenNameList.remove(0);
                        String chartValue = chartDateTto.getChartValue();
                        // 删除第一行的数据
                        getdata.remove(0);
                        // 删除每行的第一列数据
                        for (List<String> getdatum : getdata) {
                            getdatum.remove(0);
                        }
                        // 按列分组
                        int columnCount = getdata.get(0).size();
                        List<List<Double>> groupedData = new ArrayList<>();
                        for (int i = 0; i < columnCount; i++) {
                            List<Double> columnData = new ArrayList<>();
                            for (List<String> row : getdata) {
                                columnData.add(Double.parseDouble(row.get(i)));
                            }
                            groupedData.add(columnData);
                        }

                        // 计算每组数据的平均值和标准差
                        ArrayList<Double> averages = new ArrayList<>();
                        ArrayList<Double> stdDevs = new ArrayList<>();
                        for (List<Double> groupedDatum : groupedData) {
                            double sum = groupedDatum.stream().mapToDouble(Double::doubleValue).sum();
                            double average = sum / groupedDatum.size();
                            averages.add(average);
                            // 计算标准差
                            double stdDev = calculateStandardDeviation(groupedDatum, average);
                            stdDevs.add(stdDev);
                        }
                        // 将每个平均值和对应的组名称进行配对并放入 group 集合中
                        ArrayList<ArrayList<Object>> group = new ArrayList<>();
                        for (int i = 0; i < averages.size(); i++) {
                            String name = lenNameList.get(i);
                            Double average = averages.get(i);
                            ArrayList<Object> pairedData = new ArrayList<>();
                            pairedData.add(name);
                            pairedData.add(average);
                            group.add(pairedData);
                        }

                        // 计算每组数据的上限和下限（使用标准误）
                        ArrayList<Double> upperLimits = new ArrayList<>();
                        ArrayList<Double> lowerLimits = new ArrayList<>();
                        for (int i = 0; i < groupedData.size(); i++) {
                            double average = averages.get(i);
                            double stdDev = stdDevs.get(i);
                            double n = groupedData.get(i).size(); // 样本大小
                            // 计算标准误
                            double standardError = stdDev / Math.sqrt(n);
                            // 计算误差棒的上限和下限
                            double newUpper = average + standardError; // 使用标准误
                            double newLower = average - standardError; // 使用标准误
                            upperLimits.add(newUpper);
                            lowerLimits.add(newLower);
                        }
                        // 将上限和下限以数组的形式放入集合
                        ArrayList<ArrayList<Object>> limitsGroup = new ArrayList<>();
                        for (int i = 0; i < upperLimits.size(); i++) {
                            String name = lenNameList.get(i);
                            double newUpper = upperLimits.get(i);
                            double newLower = lowerLimits.get(i);
                            ArrayList<Object> limitsData = new ArrayList<>();
                            limitsData.add(name);      // 添加组名称
                            limitsData.add(newUpper);  // 添加新的上限
                            limitsData.add(newLower);  // 添加新的下限
                            limitsGroup.add(limitsData);
                        }
                        // 打印新的上限和下限
                        ArrayList<ArrayNode> formattedLimitsList = new ArrayList<>();
                        ObjectMapper objectMapper = new ObjectMapper();
                        for (ArrayList<Object> limitsData : limitsGroup) {
                            String name = (String) limitsData.get(0);
                            Double newUpperLimit = (Double) limitsData.get(1);
                            Double newLowerLimit = (Double) limitsData.get(2);
                            // 创建一个 JSON 数组节点
                            ArrayNode limitArrayNode = objectMapper.createArrayNode();
                            limitArrayNode.add(name);
                            limitArrayNode.add(newUpperLimit);
                            limitArrayNode.add(newLowerLimit);
                            // 打印格式化的结果
                            System.out.println(limitArrayNode.toString());
                            // 将格式化的数组添加到新集合中
                            formattedLimitsList.add(limitArrayNode);
                        }

                        // 解析 chartValue
                        JsonNode rootNode = objectMapper.readTree(chartValue);

                        // 获取 series 数组并清空第二个对象的 data 数组
                        JsonNode seriesNode = rootNode.path("series");
                        if (seriesNode.isArray() && seriesNode.size() > 1) {
                            JsonNode secondSeriesNode = seriesNode.get(1);
                            JsonNode dataNode = secondSeriesNode.path("data");
                            if (dataNode.isArray()) {
                                // 清空 data 数组
                                ((ArrayNode) dataNode).removeAll(); // 删除所有元素

                                // 将 formattedLimitsList 的内容添加到 data 数组中
                                for (ArrayNode limit : formattedLimitsList) {
                                    ((ArrayNode) dataNode).add(limit);
                                }
                            }
                        }
                        // 获取 dataset 数组并清空内容
                        JsonNode datasetNode = rootNode.path("dataset");
                        if (datasetNode.isArray() && datasetNode.size() > 0) {
                            JsonNode sourceNode = datasetNode.get(0).path("source");
                            if (sourceNode.isArray()) {
                                // 清空 source 数组
                                ((ArrayNode) sourceNode).removeAll();

                                // 将 group 的内容替换到 source 数组中
                                for (Object obj : group) {
                                    ((ArrayNode) sourceNode).add(objectMapper.valueToTree(obj));
                                }
                            }
                        }

                        // 将修改后的 JSON 转换为字符串并返回
                        String updatedChartValue = objectMapper.writeValueAsString(rootNode);
                        ArrayList<ChartSmartVo> newList = new ArrayList<>();
                        ChartSmartVo chartSmartVo = new ChartSmartVo();
                        chartSmartVo.setConfigValue(updatedChartValue);
                        chartSmartVo.setUniList(lenNameList);
                        newList.add(chartSmartVo);

                        return AjaxResult.success(newList);

                    } else if (errorBarType == 2) { //置信度
                        // 提取数据
                        List<List<String>> getdata = extractDataFromListNullString(chartDateTto.getDataList());
                        List<String> lenNameList = getdata.get(0);
                        lenNameList.remove(0);
                        String chartValue = chartDateTto.getChartValue();

                        // 删除第一行的数据
                        getdata.remove(0);
                        // 删除每行的第一列数据
                        for (List<String> getdatum : getdata) {
                            getdatum.remove(0);
                        }

                        // 按列分组
                        int columnCount = getdata.get(0).size();
                        List<List<Double>> groupedData = new ArrayList<>();

                        for (int i = 0; i < columnCount; i++) {
                            List<Double> columnData = new ArrayList<>();
                            for (List<String> row : getdata) {
                                columnData.add(Double.parseDouble(row.get(i)));
                            }
                            groupedData.add(columnData);
                        }

                        // 计算每组数据的平均值和标准差
                        ArrayList<Double> averages = new ArrayList<>();
                        ArrayList<Double> stdDevs = new ArrayList<>();
                        for (List<Double> groupedDatum : groupedData) {
                            double sum = groupedDatum.stream().mapToDouble(Double::doubleValue).sum();
                            double average = sum / groupedDatum.size();
                            averages.add(average);

                            // 计算标准差
                            double stdDev = calculateStandardDeviation(groupedDatum, average);
                            stdDevs.add(stdDev);
                        }

                        // 将每个平均值和对应的组名称进行配对并放入 group 集合中
                        ArrayList<ArrayList<Object>> group = new ArrayList<>();
                        for (int i = 0; i < averages.size(); i++) {
                            String name = lenNameList.get(i);
                            Double average = averages.get(i);
                            ArrayList<Object> pairedData = new ArrayList<>();
                            pairedData.add(name);
                            pairedData.add(average);
                            group.add(pairedData);
                        }

                        // 计算每组数据的上限和下限（使用置信区间）
                        ArrayList<Double> upperLimits = new ArrayList<>();
                        ArrayList<Double> lowerLimits = new ArrayList<>();
                        double zValue = 1.96; // 95%置信水平的Z值
                        for (int i = 0; i < groupedData.size(); i++) {
                            double average = averages.get(i);
                            double stdDev = stdDevs.get(i);
                            double n = groupedData.get(i).size(); // 样本大小

                            // 计算置信区间的上限和下限
                            double marginOfError = zValue * (stdDev / Math.sqrt(n));
                            double newUpper = average + marginOfError; // 使用置信区间
                            double newLower = average - marginOfError; // 使用置信区间
                            upperLimits.add(newUpper);
                            lowerLimits.add(newLower);
                        }

                        // 将上限和下限以数组的形式放入集合
                        ArrayList<ArrayList<Object>> limitsGroup = new ArrayList<>();
                        for (int i = 0; i < upperLimits.size(); i++) {
                            String name = lenNameList.get(i);
                            double newUpper = upperLimits.get(i);
                            double newLower = lowerLimits.get(i);
                            ArrayList<Object> limitsData = new ArrayList<>();
                            limitsData.add(name);      // 添加组名称
                            limitsData.add(newUpper);  // 添加新的上限
                            limitsData.add(newLower);  // 添加新的下限
                            limitsGroup.add(limitsData);
                        }
                        // 打印新的上限和下限
                        ArrayList<ArrayNode> formattedLimitsList = new ArrayList<>();
                        ObjectMapper objectMapper = new ObjectMapper();

                        for (ArrayList<Object> limitsData : limitsGroup) {
                            String name = (String) limitsData.get(0);
                            Double newUpperLimit = (Double) limitsData.get(1);
                            Double newLowerLimit = (Double) limitsData.get(2);

                            // 创建一个 JSON 数组节点
                            ArrayNode limitArrayNode = objectMapper.createArrayNode();
                            limitArrayNode.add(name);
                            limitArrayNode.add(newUpperLimit);
                            limitArrayNode.add(newLowerLimit);

                            // 打印格式化的结果
                            System.out.println(limitArrayNode.toString());
                            // 将格式化的数组添加到新集合中
                            formattedLimitsList.add(limitArrayNode);
                        }

                        // 解析 chartValue
                        JsonNode rootNode = objectMapper.readTree(chartValue);
                        // 获取 series 数组并清空第二个对象的 data 数组
                        JsonNode seriesNode = rootNode.path("series");
                        if (seriesNode.isArray() && seriesNode.size() > 1) {
                            JsonNode secondSeriesNode = seriesNode.get(1);
                            JsonNode dataNode = secondSeriesNode.path("data");
                            if (dataNode.isArray()) {
                                // 清空 data 数组
                                ((ArrayNode) dataNode).removeAll(); // 删除所有元素
                                // 将 formattedLimitsList 的内容添加到 data 数组中
                                for (ArrayNode limit : formattedLimitsList) {
                                    ((ArrayNode) dataNode).add(limit);
                                }
                            }
                        }
                        // 获取 dataset 数组并清空内容
                        JsonNode datasetNode = rootNode.path("dataset");
                        if (datasetNode.isArray() && datasetNode.size() > 0) {
                            JsonNode sourceNode = datasetNode.get(0).path("source");
                            if (sourceNode.isArray()) {
                                // 清空 source 数组
                                ((ArrayNode) sourceNode).removeAll();

                                // 将 group 的内容替换到 source 数组中
                                for (Object obj : group) {
                                    ((ArrayNode) sourceNode).add(objectMapper.valueToTree(obj));
                                }
                            }
                        }

                        // 将修改后的 JSON 转换为字符串并返回
                        String updatedChartValue = objectMapper.writeValueAsString(rootNode);
                        ArrayList<ChartSmartVo> newList = new ArrayList<>();
                        ChartSmartVo chartSmartVo = new ChartSmartVo();
                        chartSmartVo.setConfigValue(updatedChartValue);
                        chartSmartVo.setUniList(lenNameList);
                        newList.add(chartSmartVo);
                        return AjaxResult.success(newList);
                    }

                }

                String chartValue = chartDateTto.getChartValue();
                List<List<String>> lists = extractDataFromList(chartDateTto.getDataList());

                ObjectMapper objectMapper = new ObjectMapper();
                JsonNode chartValueJson = objectMapper.readTree(chartValue);

                // 获取dataset节点并替换source节点
                ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
                for (JsonNode node : dataset) {
                    if (node.has("source")) {
                        ((ObjectNode) node).set("source", objectMapper.valueToTree(lists));
                        break;
                    }
                }
                // 移除集合中第一个对象的第一个元素
                if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
                    lists.get(0).remove(0);
                }

                // 获取series节点并替换name字段
                ArrayNode series = (ArrayNode) chartValueJson.path("series");
                List<String> names = lists.isEmpty() ? Collections.emptyList() : lists.get(0);
                for (int i = 0; i < series.size() && i < names.size(); i++) {
                    JsonNode seriesItem = series.get(i);
                    if (seriesItem.has("name")) {
                        ((ObjectNode) seriesItem).put("name", names.get(i));
                    }
                }


                // 获取legend节点并更新data
                JsonNode legend = chartValueJson.path("legend");
                ChartDataUtils.updateLegendData(legend, lists);
                String updatedChartValue = chartValueJson.toString();
                ArrayList<ChartSmartVo> newList = new ArrayList<>();

                // 处理颜色字段
                ObjectNode jsonNode = (ObjectNode) objectMapper.readTree(chartValue);
                // 获取 color 字段
                JsonNode colorNode = jsonNode.path("color");

                List<String> baseColors = new ArrayList<>();
                // 遍历 color 数组并将每个颜色值添加到 baseColors 集合中
                if (colorNode.isArray()) {
                    for (JsonNode color : colorNode) {
                        String colorValue = color.asText();
                        baseColors.add(colorValue);
                    }
                }

                int beforeSize = baseColors.size();
                int afterSize = lists.get(0).size();
                if (beforeSize < afterSize) {
                    chartValueJson = ColorUtils.processChartColorsHert(JSON.toJSONString(chartValueJson), afterSize);
                }

                // 获取ChartConfigs对象
                ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
                // 创建新的ChartSmartVo对象并设置属性
                ChartSmartVo chartSmartVo = new ChartSmartVo();
                chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
                chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
                chartSmartVo.setConfigValue(updatedChartValue);
                chartSmartVo.setChartTypeId(configs.getChartTypeId());
                newList.add(chartSmartVo);
                System.out.println(updatedChartValue);
                return AjaxResult.success(newList);
            } else {
                if (chartDateTto.getConfigId() == 44 || chartDateTto.getConfigId() == 42) {
                    List<List<String>> getdata = extractDataFromList(chartDateTto.getDataList());
                    List<Map> roomName = new ArrayList<>();
                    List<String> strings = getdata.get(1);
                    HashMap<String, String> map = new HashMap<>();
                    map.put("name", strings.get(0));
                    roomName.add(map);
                    ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
                    String chartValue = configs.getConfigValue();
                    List<List<String>> lists = extractDataFromList(chartDateTto.getDataList());
                    ObjectMapper objectMapper = new ObjectMapper();
                    JsonNode chartValueJson = objectMapper.readTree(chartValue);
                    // 获取dataset节点并替换source节点
                    JsonNode datasetNode = chartValueJson.path("dataset");
                    if (datasetNode.isArray()) {
                        ArrayNode dataset = (ArrayNode) datasetNode;
                        for (JsonNode node : dataset) {
                            if (node.has("source")) {
                                ((ObjectNode) node).set("source", objectMapper.valueToTree(lists));
                                break;
                            }
                        }
                    } else if (datasetNode.isObject()) {
                        ObjectNode dataset = (ObjectNode) datasetNode;
                        if (dataset.has("source")) {
                            dataset.set("source", objectMapper.valueToTree(lists));
                        }
                    }
                    // 移除集合中第一个对象的第一个元素
                    if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
                        lists.get(0).remove(0);
                    }
                    // 获取series节点并替换name字段
                    ArrayNode series = (ArrayNode) chartValueJson.path("series");
                    List<String> names = lists.isEmpty() ? Collections.emptyList() : lists.get(0);
                    for (int i = 0; i < series.size() && i < names.size(); i++) {
                        JsonNode seriesItem = series.get(i);
                        if (seriesItem.has("name")) {
                            ((ObjectNode) seriesItem).put("name", names.get(i));
                        }
                    }

                    // 获取legend节点并更新data
                    JsonNode legend = chartValueJson.path("legend");
                    ChartDataUtils.updateLegendData(legend, lists);
                    // 处理颜色字段
                    ObjectNode jsonNode = (ObjectNode) objectMapper.readTree(chartValue);
                    // 获取 color 字段
                    JsonNode colorNode = jsonNode.path("color");
                    List<String> baseColors = new ArrayList<>();
                    // 遍历 color 数组并将每个颜色值添加到 baseColors 集合中
                    if (colorNode.isArray()) {
                        for (JsonNode color : colorNode) {
                            String colorValue = color.asText();
                            baseColors.add(colorValue);
                        }
                    }

                    String updatedChartValue = chartValueJson.toString();
                    ArrayList<ChartSmartVo> newList = new ArrayList<>();

                    // 获取ChartConfigs对象
                    // 创建新的ChartSmartVo对象并设置属性
                    ChartSmartVo chartSmartVo = new ChartSmartVo();
                    chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
                    chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
                    chartSmartVo.setConfigValue(updatedChartValue);
                    chartSmartVo.setChartTypeId(configs.getChartTypeId());

                    newList.add(chartSmartVo);
                    System.out.println(updatedChartValue);
                    return AjaxResult.success(newList);

                }

                //chart_configs_interface的config_id 等于 chart_configs的id字段
                Integer id = chartDateTto.getConfigId();
                //根据id查询chart_configs
                ChartConfigs chartConfigs = chartConfigsService.getById(id);
                //模版
                String configValue = chartConfigs.getConfigValue();
                List<List<String>> lists = extractDataFromList(chartDateTto.getDataList());
                ObjectMapper objectMapper = new ObjectMapper();
                JsonNode chartValueJson = objectMapper.readTree(configValue);
                // 获取dataset节点并替换source节点
                ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
                for (JsonNode node : dataset) {
                    if (node.has("source")) {
                        ((ObjectNode) node).set("source", objectMapper.valueToTree(lists));
                        break;
                    }
                }

                // 移除集合中第一个对象的第一个元素
                if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
                    lists.get(0).remove(0);
                }
                // 获取series节点并替换name字段
                ArrayNode series = (ArrayNode) chartValueJson.path("series");
                List<String> names = lists.isEmpty() ? Collections.emptyList() : lists.get(0);
                for (int i = 0; i < series.size() && i < names.size(); i++) {
                    JsonNode seriesItem = series.get(i);
                    if (seriesItem.has("name")) {
                        ((ObjectNode) seriesItem).put("name", names.get(i));
                    }
                }
                // 获取legend节点并更新data
                JsonNode legend = chartValueJson.path("legend");
                ChartDataUtils.updateLegendData(legend, lists);
                String updatedChartValue = chartValueJson.toString();
                ArrayList<ChartSmartVo> newList = new ArrayList<>();
                // 获取ChartConfigs对象
                ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
                // 创建新的ChartSmartVo对象并设置属性
                ChartSmartVo chartSmartVo = new ChartSmartVo();
                chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
                chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
                chartSmartVo.setConfigValue(updatedChartValue);
                chartSmartVo.setChartTypeId(configs.getChartTypeId());
                newList.add(chartSmartVo);
                System.out.println(updatedChartValue);
                return AjaxResult.success(newList);
            }
        } catch (Exception e) {
            log.error("importData7接口的configId:{}发生异常:{}", chartDateTto.getConfigId(), e);
            throw new RuntimeException(e);
        }

    }


    @PostMapping("/ai/fillData")
    @Transactional(rollbackFor = Exception.class) // 添加事务管理注解，捕获所有异常回滚事务
    public AjaxResult aiFillData(@RequestBody ChartDateTto chartDateTto) throws Exception {
        Integer configId = chartDateTto.getConfigId();
        String chartValue = chartDateTto.getChartValue();
        if (configId == 4) {
            // Extract data from the provided list
            List<List<String>> lists = extractDataFromList(chartDateTto.getDataList());
            // Initialize ObjectMapper to handle JSON processing
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode chartValueJson = objectMapper.readTree(chartValue);

            // Get the 'dataset' node
            JsonNode datasetNode = chartValueJson.path("dataset");
            if (datasetNode.isArray()) {
                ArrayNode datasetArray = (ArrayNode) datasetNode;
                for (JsonNode node : datasetArray) {
                    if (node.has("source")) {
                        ((ObjectNode) node).set("source", objectMapper.valueToTree(lists));
                        break;
                    }
                }
            } else if (datasetNode.isObject()) {
                ObjectNode datasetObject = (ObjectNode) datasetNode;
                if (datasetObject.has("source")) {
                    datasetObject.set("source", objectMapper.valueToTree(lists));
                }
            }

            // Remove the first element of the first list
            if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
                lists.get(0).remove(0);
            }

            // Get the 'series' node and replace the 'name' field
            JsonNode seriesNode = chartValueJson.path("series");
            if (seriesNode.isArray()) {
                ArrayNode series = (ArrayNode) seriesNode;
                List<String> names = lists.isEmpty() ? Collections.emptyList() : lists.get(0);
                for (int i = 0; i < series.size() && i < names.size(); i++) {
                    JsonNode seriesItem = series.get(i);
                    if (seriesItem.has("name")) {
                        ((ObjectNode) seriesItem).put("name", names.get(i));
                    }
                }
            }

            // Get the 'legend' node and update the 'data' field
            JsonNode legend = chartValueJson.path("legend");
            ChartDataUtils.updateLegendData(legend, lists);

            // Convert the updated JSON back to a string
            String updatedChartValue = chartValueJson.toString();

            // Create a new list and add the updated chart config
            ArrayList<ChartSmartVo> newLista = new ArrayList<>();
            ChartSmartVo chartSmartVo = new ChartSmartVo();
            chartSmartVo.setConfigValue(updatedChartValue);
            newLista.add(chartSmartVo);
            return AjaxResult.success(newLista);

            //柱状图
        } else if (configId == 39) { //饼图

            // 获取原始数据列表并移除第一个对象
            List<Map<String, Object>> originalMapList = chartDateTto.getDataList();
            if (!originalMapList.isEmpty()) {
                originalMapList.remove(0);
            }

            // 将 List<Map<String, Object>> 转换为 List<List<Object>>
            List<List<Object>> lists = convertDataList(originalMapList);

            // 从第二个list开始，每个list的元素除了第一个，其他转换为数字类型
            for (int i = 1; i < lists.size(); i++) {
                List<Object> row = lists.get(i);
                for (int j = 1; j < row.size(); j++) {
                    if (row.get(j) instanceof String) {
                        try {
                            row.set(j, Double.parseDouble((String) row.get(j)));
                        } catch (NumberFormatException e) {
                            // 如果转换失败，保留原字符串
                        }
                    }
                }
            }

            // 移除第一个对象
            List<List<Object>> modifiedLists = new ArrayList<>(lists);
            if (!modifiedLists.isEmpty()) {
                modifiedLists.remove(0);
            }

            // 解析 chartValue JSON 字符串
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode chartValueJson = objectMapper.readTree(chartValue);

            // 获取 dataset 节点，并检查其是否为 ObjectNode 或 ArrayNode 类型
            JsonNode datasetNode = chartValueJson.path("dataset");

            if (datasetNode.isArray()) {
                ArrayNode dataset = (ArrayNode) datasetNode;

                // 找到 source 节点并替换它，保留第一个对象
                for (JsonNode node : dataset) {
                    if (node.has("source")) {
                        ArrayNode originalSource = (ArrayNode) node.get("source");
                        ArrayNode newSource = objectMapper.createArrayNode();

                        // 保留原始 source 的第一个对象
                        if (originalSource.size() > 0) {
                            newSource.add(originalSource.get(0));
                        }

                        // 将修改后的数据逐项添加到 newSource 中
                        ArrayNode modifiedSource = objectMapper.valueToTree(modifiedLists);
                        newSource.addAll(modifiedSource);

                        // 替换 source 节点
                        ((ObjectNode) node).set("source", newSource);
                        break;
                    }
                }

            } else if (datasetNode.isObject()) {
                ObjectNode dataset = (ObjectNode) datasetNode;

                if (dataset.has("source")) {
                    ArrayNode originalSource = (ArrayNode) dataset.get("source");
                    ArrayNode newSource = objectMapper.createArrayNode();

                    // 保留原始 source 的第一个对象
                    if (originalSource.size() > 0) {
                        newSource.add(originalSource.get(0));
                    }

                    // 将修改后的数据逐项添加到 newSource 中
                    ArrayNode modifiedSource = objectMapper.valueToTree(modifiedLists);
                    newSource.addAll(modifiedSource);

                    // 替换 source 节点
                    dataset.set("source", newSource);
                }

            } else {
                throw new Exception("Dataset is neither an array nor an object in the provided JSON.");
            }

            // 将更新后的 JSON 转换为字符串
            String updatedChartValue = chartValueJson.toString();

            // 返回结果
            ArrayList<ChartSmartVo> newListc = new ArrayList<>();
            ChartSmartVo chartSmartVo = new ChartSmartVo();
            chartSmartVo.setConfigValue(updatedChartValue);
            newListc.add(chartSmartVo);

            return AjaxResult.success(newListc);

        } else if (configId == 11) {
            List<List<String>> lists = ChartDataUtils.extractDataFromList(chartDateTto.getDataList());
            int size = lists.get(0).size(); // 假设所有列长度一致
            int group = size / 2;
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode chartValueJson = objectMapper.readTree(chartValue);

            // 获取 dataset 节点并确保是 ArrayNode 类型
            JsonNode datasetNode = chartValueJson.path("dataset");
            ArrayNode dataset;
            if (datasetNode.isArray()) {
                dataset = (ArrayNode) datasetNode;
            } else {
                // 如果 dataset 节点不是 ArrayNode，则创建一个新的 ArrayNode
                dataset = objectMapper.createArrayNode();
                ((ObjectNode) chartValueJson).set("dataset", dataset);
            }

            // 清空 dataset 节点
            dataset.removeAll();

            // 根据 group 的数量重新建立 source
            for (int i = 0; i < group; i++) {
                ArrayNode newSource = objectMapper.createArrayNode();
                for (List<String> listq : lists) {
                    if (listq.size() > i * 2) {
                        ArrayNode pair = objectMapper.createArrayNode();
                        pair.add(listq.get(i * 2)); // 第一个数据
                        if (listq.size() > i * 2 + 1) {
                            pair.add(listq.get(i * 2 + 1)); // 第二个数据
                        }
                        newSource.add(pair);
                    }
                }
                ObjectNode newDatasetObject = objectMapper.createObjectNode();
                newDatasetObject.set("source", newSource);
                dataset.add(newDatasetObject);
            }

            // 获取 series 节点并确保是 ArrayNode 类型
            JsonNode seriesNode = chartValueJson.path("series");
            ArrayNode seriesArray;
            if (seriesNode.isArray()) {
                seriesArray = (ArrayNode) seriesNode;
            } else {
                // 如果 series 节点不是 ArrayNode，则创建一个新的 ArrayNode
                seriesArray = objectMapper.createArrayNode();
                ((ObjectNode) chartValueJson).set("series", seriesArray);
            }

            // 清空 series 节点
            seriesArray.removeAll();

            // 重新构建 series，每两个数据为一组
            for (int i = 0; i < group; i++) {
                ObjectNode seriesObject = objectMapper.createObjectNode();
                seriesObject.put("name", lists.get(0).get(i * 2)); // 使用第一个数据作为 name
                seriesObject.put("type", "scatter"); // 假设图表类型为 scatter，可以根据需要调整
                seriesObject.put("datasetIndex", i);
                seriesArray.add(seriesObject);
            }

            // 获取 legend 节点并更新 data
            JsonNode legend = chartValueJson.path("legend");
            ChartDataUtils.updateLegendData(legend, lists);
            // 将更新后的 JSON 转换为字符串
            String updatedChartValue = chartValueJson.toString();
            // 创建返回结果
            ArrayList<ChartSmartVo> newListd = new ArrayList<>();
            ChartSmartVo chartSmartVo = new ChartSmartVo();
            chartSmartVo.setConfigValue(updatedChartValue);
            newListd.add(chartSmartVo);
            return AjaxResult.success(newListd);
        } else if (configId == 45) {
            List<List<String>> lists = extractDataFromList(chartDateTto.getDataList());
            System.out.println(chartValue);
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode chartValueJson = objectMapper.readTree(chartValue);
            // 获取 dataset 节点并检查类型
            JsonNode datasetNode = chartValueJson.path("dataset");
            if (datasetNode.isArray()) {
                // 如果 dataset 是数组
                ArrayNode dataset = (ArrayNode) datasetNode;
                for (JsonNode node : dataset) {
                    if (node.has("source")) {
                        ((ObjectNode) node).set("source", objectMapper.valueToTree(lists));
                        break;
                    }
                }
            } else if (datasetNode.isObject()) {
                // 如果 dataset 是对象
                ObjectNode dataset = (ObjectNode) datasetNode;
                if (dataset.has("source")) {
                    dataset.set("source", objectMapper.valueToTree(lists));
                }
            } else {
                throw new IllegalArgumentException("The 'dataset' field is not an array or object.");
            }

            // 移除集合中第一个对象的第一个元素
            if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
                lists.get(0).remove(0);
            }

            JsonNode seriesNode = chartValueJson.path("series");
            if (seriesNode.isArray()) {
                ArrayNode seriesArray = (ArrayNode) seriesNode;
                // 计算第一个对象元素数量
                int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();
                System.out.println(firstObjectRemainingElements);

                // 如果 series 字段的对象数量小于第一个集合的数量，循环复制 series 中的对象使其数量相等
                if (seriesArray.size() < firstObjectRemainingElements) {
                    JsonNode firstSeriesObject = seriesArray.get(0);
                    for (int i = seriesArray.size(); i < firstObjectRemainingElements; i++) {
                        seriesArray.add(firstSeriesObject.deepCopy());
                    }
                } else {
                    // 删除多余的对象
                    while (seriesArray.size() > firstObjectRemainingElements) {
                        seriesArray.remove(seriesArray.size() - 1);
                    }
                }
                // 更新 series 中每个对象的 name 字段
                for (int i = 0; i < firstObjectRemainingElements; i++) {
                    ((ObjectNode) seriesArray.get(i)).put("name", lists.get(0).get(i));
                }
            } else if (seriesNode.isObject()) {
                // 处理 series 为 ObjectNode 的情况（如果需要）
                throw new IllegalArgumentException("The 'series' field is not an array but an object.");
            } else {
                throw new IllegalArgumentException("The 'series' field is not an array or object.");
            }

            // 获取 legend 节点并更新 data
            JsonNode legend = chartValueJson.path("legend");
            ChartDataUtils.updateLegendData(legend, lists);

            // 生成更新后的 JSON 字符串
            String updatedChartValue = chartValueJson.toString();

            // 返回结果
            ArrayList<ChartSmartVo> newListe = new ArrayList<>();
            ChartSmartVo chartSmartVo = new ChartSmartVo();
            chartSmartVo.setConfigValue(updatedChartValue);
            newListe.add(chartSmartVo);
            return AjaxResult.success(newListe);

        } else if (configId == 58 || configId == 38) {//饼图

            //根据id查询chart_configs
            String chartValuen = chartDateTto.getChartValue();

            List<Map<String, Object>> originalMapList = chartDateTto.getDataList();

            // 移除第一个对象
            if (!originalMapList.isEmpty()) {
                originalMapList.remove(0);
            }

            // 将 List<Map<String, Object>> 转换为 List<List<Object>>
            List<List<Object>> lists = convertDataList(originalMapList);
            // 从第二个list开始，每个list的元素除了第一个，其他转换为整数类型
            for (int i = 1; i < lists.size(); i++) {
                List<Object> row = lists.get(i);
                for (int j = 1; j < row.size(); j++) {
                    if (row.get(j) instanceof String) {
                        try {
                            row.set(j, Double.parseDouble((String) row.get(j)));
                        } catch (NumberFormatException e) {
                            // 如果转换失败，保留原字符串
                        }
                    }
                }
            }
            // 移除第一个对象
            List<List<Object>> modifiedLists = new ArrayList<>(lists);
            if (!modifiedLists.isEmpty()) {
                modifiedLists.remove(0);
            }
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode chartValueJson = objectMapper.readTree(chartValuen);
            // 获取 dataset 节点
            ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
            // 找到 source 节点并替换它，保留第一个对象
            for (JsonNode node : dataset) {
                if (node.has("source")) {
                    ArrayNode originalSource = (ArrayNode) node.get("source");
                    ArrayNode newSource = objectMapper.createArrayNode();
                    // 保留第一个对象
                    if (originalSource.size() > 0) {
                        newSource.add(originalSource.get(0));
                    }
                    // 将修改后的数据逐项添加到 newSource 中
                    ArrayNode modifiedSource = objectMapper.valueToTree(modifiedLists);
                    for (JsonNode jsonNode : modifiedSource) {
                        newSource.add(jsonNode);
                    }
                    ((ObjectNode) node).set("source", newSource);
                    break;
                }
            }
            String updatedChartValue = chartValueJson.toString();
            ArrayList<ChartSmartVo> newList = new ArrayList<>();
            // 获取 ChartConfigs 对象
            ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
            if (ObjectUtils.isEmpty(configs)) {
                log.error("未找到图表配置。配置ID={}", chartDateTto.getConfigId());
                return AjaxResult.error("未找到该图表的配置，请检查配置ID是否正确。");
            }
            // 创建新的 ChartSmartVo 对象并设置属性
            ChartSmartVo chartSmartVo = new ChartSmartVo();
            chartSmartVo.setConfigId(configs.getId()); // 假设 ChartConfigs 有 getId 方法
            chartSmartVo.setConfigName(configs.getConfigName()); // 假设 ChartConfigs 有 getName 方法
            chartSmartVo.setConfigValue(updatedChartValue);
            chartSmartVo.setChartTypeId(configs.getChartTypeId());
            newList.add(chartSmartVo);
            System.out.println(updatedChartValue);
            return AjaxResult.success(newList);
        } else if (configId == 5) {//心电图曲线图
            //String chartValue = chartDateTto.getChartValue();
            List<List<String>> lists = ChartDataUtils.extractDataFromList(chartDateTto.getDataList());
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode chartValueJson = objectMapper.readTree(chartValue);

            // 获取dataset节点
            ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");

            System.out.println("我是dataset" + dataset);

            for (List<String> list : lists) {
                System.out.println("我是list" + list);
            }
            // 更新dataset中的source节点
            dataset = ChartDataUtils.updateDatasetSourcesCyclic(dataset, lists, objectMapper);

            // 移除集合中第一个对象的第一个元素
            if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
                lists.get(0).remove(0);
            }


            // 获取series字段并删除其余对象
            ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");
            if (seriesArray.size() > 1) {
                while (seriesArray.size() > 1) {
                    seriesArray.remove(1);
                }
            }

            // 计算第一个对象剩余元素数量
            int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();

            // 复制series的第一个对象
            JsonNode firstSeriesObject = seriesArray.get(0);
            for (int i = 1; i < firstObjectRemainingElements; i++) {
                seriesArray.add(firstSeriesObject.deepCopy());
            }

            // 在这里添加新的逻辑来处理 datasetIndex 字段
            for (int i = 0; i < seriesArray.size(); i++) {
                ObjectNode seriesObject = (ObjectNode) seriesArray.get(i);
                if (seriesObject.has("datasetIndex")) {
                    seriesObject.put("datasetIndex", i);
                } else {
                    seriesObject.put("datasetIndex", i);
                }
            }

            // 获取legend节点并更新data
            JsonNode legend = chartValueJson.path("legend");
            ChartDataUtils.updateLegendData(legend, lists);

            // 更新series中每个对象的name字段
            for (int i = 0; i < firstObjectRemainingElements; i++) {
                ((ObjectNode) seriesArray.get(i)).put("name", lists.get(0).get(i));
            }

            String updatedChartValue = chartValueJson.toString();

            ArrayList<ChartSmartVo> newList = new ArrayList<>();
            // 获取ChartConfigs对象
            ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
            // 创建新的ChartSmartVo对象并设置属性
            ChartSmartVo chartSmartVo = new ChartSmartVo();
            chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
            chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
            chartSmartVo.setConfigValue(updatedChartValue);
            chartSmartVo.setChartTypeId(configs.getChartTypeId());
            newList.add(chartSmartVo);
            System.out.println(updatedChartValue);
            return AjaxResult.success(newList);


        } else if (configId == 47) { //线性回归图


            JSONObject jsonObject = JSON.parseObject(chartValue);
            // String chartValue = chartDateTto.getChartValue();
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode chartValueJson = objectMapper.readTree(chartValue);
            System.out.println("传入的数据: " + jsonObject + " 传入的数据");

            List<List<String>> getdata = extractDataFromList(chartDateTto.getDataList());

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> groupName = new ArrayList<>();
            List<List<List<String>>> newList = new ArrayList<>();
            List<List<String>> list = new ArrayList<>();

            for (int i = 1; i < getdata.size(); i++) {
                if (i == getdata.size() - 1) {
                    List<String> sdata = getdata.get(i);
                    list.add(sdata);
                    newList.add(list);
                } else if (i == 1) {
                    List<String> gName = getdata.get(i);
                    groupName.add(gName.get(0));
                } else {
                    if (getdata.get(i).size() == 1) {
                        List<String> gName = getdata.get(i);
                        groupName.add(gName.get(0));
                        newList.add(list);
                        list = new ArrayList<>();
                    } else {
                        List<String> sdata = getdata.get(i);
                        ArrayList<String> newList1 = new ArrayList<>();
                        for (int i1 = 0; i1 < sdata.size(); i1++) {
                            String s = sdata.get(i1);
                            if (org.apache.commons.lang3.ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                                newList1.add(s);
                            }
                        }
                        list.add(newList1);
                    }
                }
            }

            ArrayList<Map> maps = new ArrayList<>();

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject json = series.get(0);

            ArrayList<JSONObject> jsonObjects = new ArrayList<>();

            for (int i = 0; i < newList.size(); i++) {
                List<List<String>> lists = newList.get(i);
                List<List<Object>> nonStringLists = new ArrayList<>();

                for (List<String> sublist : lists) {
                    List<Object> nonStringSublist = new ArrayList<>();
                    for (String item : sublist) {
                        try {
                            // 尝试将字符串转换为数字
                            nonStringSublist.add(Double.parseDouble(item));
                        } catch (NumberFormatException e) {
                            // 如果转换失败，保留原字符串
                            nonStringSublist.add(item);
                        }
                    }
                    nonStringLists.add(nonStringSublist);
                }

                HashMap<String, Object> map = new HashMap<>();
                map.put("source", nonStringLists);
                maps.add(map);

                double[] x = new double[lists.size()];
                double[] y = new double[lists.size()];

                for (int i1 = 0; i1 < lists.size(); i1++) {
                    List<String> sources = lists.get(i1);
                    for (int i2 = 0; i2 < sources.size(); i2++) {
                        x[i1] = Double.parseDouble(sources.get(0));
                        y[i1] = Double.parseDouble(sources.get(1));
                    }
                }
                Map service = ExcelDataUtils.getService1(x, y);
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.putAll(json);
                jsonObject1.put("datasetIndex", i);
                jsonObject1.put("name", groupName.get(i));
                JSONObject markLine1 = (JSONObject) jsonObject1.get("markLine");
                JSONObject markLine = new JSONObject();
                markLine.putAll(markLine1);
                JSONObject label1 = (JSONObject) markLine.get("label");
                JSONObject label = new JSONObject();
                label.putAll(label1);
                label.put("formatter", service.get("formatter"));
                markLine.put("label", label);

                JSONObject tooltip1 = (JSONObject) markLine.get("tooltip");
                JSONObject tooltip = new JSONObject();
                tooltip.putAll(tooltip1);
                tooltip.put("formatter", service.get("formatter"));
                markLine.put("tooltip", tooltip);

                List<JSONObject> data = (List<JSONObject>) markLine.get("data");
                List<JSONObject> data1 = (List<JSONObject>) data.get(0);
                List<JSONObject> jsonObjects1 = new ArrayList<>();
                List<List<JSONObject>> jsonObjects2 = new ArrayList<>();
                for (int i1 = 0; i1 < data1.size(); i1++) {
                    JSONObject jsonObject2 = data1.get(i1);
                    JSONObject jsonObject3 = new JSONObject();
                    jsonObject3.putAll(jsonObject2);
                    if (i1 == 0) {
                        jsonObject3.put("coord", service.get("min"));
                    } else {
                        jsonObject3.put("coord", service.get("max"));
                    }
                    jsonObjects1.add(jsonObject3);
                }
                jsonObjects2.add(jsonObjects1);
                markLine.put("data", jsonObjects2);
                jsonObject1.put("markLine", markLine);
                jsonObjects.add(jsonObject1);
            }

            // 获取 legend 数组并处理
            JSONArray legendArray = jsonObject.getJSONArray("legend");
            if (legendArray != null && !legendArray.isEmpty()) {
                JSONObject legend = legendArray.getJSONObject(0); // 获取第一个 legend 对象
                ArrayList<Map> maps1 = new ArrayList<>();
                for (int i = 0; i < groupName.size(); i++) {
                    Map map = new HashMap<>();
                    map.put("name", groupName.get(i));
                    maps1.add(map);
                }
                legend.put("data", maps1);
                jsonObject.put("legend", legendArray);
            }

            jsonObject.put("series", jsonObjects);
            jsonObject.put("dataset", maps);
            System.out.println(JSON.toJSONString(jsonObject));

            ChartSmartVo chartSmartVo = new ChartSmartVo();

            chartSmartVo.setConfigValue(JSON.toJSONString(jsonObject));

            List<ChartSmartVo> newLista = new ArrayList<>();
            newLista.add(chartSmartVo);

            return AjaxResult.success(newLista);

        } else if (configId == 8) {

            // String chartValue = chartDateTto.getChartValue();
            List<List<String>> lists = extractDataFromList(chartDateTto.getDataList());
            System.out.println(chartValue);
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode chartValueJson = objectMapper.readTree(chartValue);
            // 获取dataset节点并替换source节点
            ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
            for (JsonNode node : dataset) {
                if (node.has("source")) {
                    ((ObjectNode) node).set("source", objectMapper.valueToTree(lists));
                    break;
                }
            }

            // 移除集合中第一个对象的第一个元素
            if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
                lists.get(0).remove(0);
            }
            ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");

            // 计算第一个对象元素数量
            int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();
            System.out.println(firstObjectRemainingElements);
            // 如果series字段的对象数量小于第一个集合的数量，循环复制series中的对象使其数量相等
            if (seriesArray.size() < firstObjectRemainingElements) {
                JsonNode firstSeriesObject = seriesArray.get(0);
                for (int i = seriesArray.size(); i < firstObjectRemainingElements; i++) {
                    seriesArray.add(firstSeriesObject.deepCopy());
                }
            } else {
                // 删除多余的对象
                while (seriesArray.size() > firstObjectRemainingElements) {
                    seriesArray.remove(seriesArray.size() - 1);
                }
            }

            // 更新series中每个对象的name字段
            for (int i = 0; i < firstObjectRemainingElements; i++) {
                ((ObjectNode) seriesArray.get(i)).put("name", lists.get(0).get(i));
            }

            // 获取legend节点并更新data
            JsonNode legend = chartValueJson.path("legend");
            ChartDataUtils.updateLegendData(legend, lists);

            String updatedChartValue = chartValueJson.toString();
            ArrayList<ChartSmartVo> newList = new ArrayList<>();
            // 获取ChartConfigs对象
            ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
            // 创建新的ChartSmartVo对象并设置属性
            ChartSmartVo chartSmartVo = new ChartSmartVo();
            chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
            chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
            chartSmartVo.setConfigValue(updatedChartValue);
            chartSmartVo.setChartTypeId(configs.getChartTypeId());

            newList.add(chartSmartVo);
            System.out.println(updatedChartValue);
            return AjaxResult.success(newList);
        }
        return AjaxResult.error("该类型未匹配");
    }


    //新接口
    @ApiOperation(" 多排3D柱状图 多线条圆滑折线图 心电图曲线图 多层圆滑折线图 水平条形柱状图 雷达图 数据组柱状图 阶梯折线图 归一柱状图 堆叠折线图")
    @PostMapping("/importData2")
    @Transactional(rollbackFor = Exception.class) // 添加事务管理注解，捕获所有异常回滚事务
    public AjaxResult importData2(@RequestBody ChartDateTto chartDateTto) throws Exception {
        // 参数校验
        String validationMessage = ChartValidator.check(chartDateTto);
        if (validationMessage != null) {
            // 返回校验失败的提示信息给前端
            return AjaxResult.error(validationMessage);
        }
        try {
            if (chartDateTto.getChartValue() != null) {
                if (chartDateTto.getConfigId() == 51) {
                    String chartValue = chartDateTto.getChartValue();
                    List<List<String>> lists = extractDataFromList(chartDateTto.getDataList());
                    ObjectMapper objectMapper = new ObjectMapper();
                    JsonNode chartValueJson = objectMapper.readTree(chartValue);

                    // 获取dataset节点并替换source节点
                    ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
                    for (JsonNode node : dataset) {
                        if (node.has("source")) {
                            ((ObjectNode) node).set("source", objectMapper.valueToTree(lists));
                            break;
                        }
                    }

                    // 移除集合中第一个对象的第一个元素
                    if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
                        lists.get(0).remove(0);
                    }

                    // 获取series字段并删除其余对象
                    ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");
                    if (seriesArray.size() > 1) {
                        while (seriesArray.size() > 1) {
                            seriesArray.remove(1);
                        }
                    }

                    // 计算第一个对象剩余元素数量
                    int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();

                    // 复制series的第一个对象
                    JsonNode firstSeriesObject = seriesArray.get(0);
                    for (int i = 1; i < firstObjectRemainingElements; i++) {
                        seriesArray.add(firstSeriesObject.deepCopy());
                    }
                    // 更新series中每个对象的name字段
                    for (int i = 0; i < firstObjectRemainingElements; i++) {
                        ObjectNode seriesObject = (ObjectNode) seriesArray.get(i);
                        seriesObject.put("name", lists.get(0).get(i));
                    }

                    // 删除每个series对象的step字段的值
                    for (int i = 0; i < seriesArray.size(); i++) {
                        ObjectNode seriesObject = (ObjectNode) seriesArray.get(i);
                        if (seriesObject.has("step")) {
                            seriesObject.remove("step");
                        }
                    }

                    // 对series中每个对象的step字段赋值
                    String[] stepValues = {"start", "middle", "end"};
                    for (int i = 0; i < seriesArray.size(); i++) {
                        ObjectNode seriesObject = (ObjectNode) seriesArray.get(i);
                        seriesObject.put("step", stepValues[i % stepValues.length]);
                    }

                    // 遍历series字段中的对象，判断是否有name字段，如果有则将其值赋给该对象的encode的y字段
                    for (int i = 0; i < seriesArray.size(); i++) {
                        ObjectNode seriesObject = (ObjectNode) seriesArray.get(i);
                        if (seriesObject.has("name")) {
                            String nameValue = seriesObject.get("name").asText();
                            if (!seriesObject.has("encode")) {
                                seriesObject.set("encode", objectMapper.createObjectNode());
                            }
                            ((ObjectNode) seriesObject.get("encode")).put("y", nameValue);
                        }
                    }

                    // 获取legend节点并更新data
                    JsonNode legend = chartValueJson.path("legend");
                    ChartDataUtils.updateLegendData(legend, lists);


                    // 处理颜色字段
                    ObjectNode jsonNode = (ObjectNode) objectMapper.readTree(chartValue);
                    // 获取 color 字段
                    JsonNode colorNode = jsonNode.path("color");
                    List<String> baseColors = new ArrayList<>();
                    // 遍历 color 数组并将每个颜色值添加到 baseColors 集合中
                    if (colorNode.isArray()) {
                        for (JsonNode color : colorNode) {
                            String colorValue = color.asText();
                            baseColors.add(colorValue);
                        }
                    }

                    int beforeSize = baseColors.size();
                    int afterSize = lists.get(0).size();
                    if (beforeSize < afterSize) {
                        chartValueJson = ColorUtils.processChartColorsHert(JSON.toJSONString(chartValueJson), afterSize);
                    }

                    String updatedChartValue = chartValueJson.toString();
                    ArrayList<ChartSmartVo> newList = new ArrayList<>();

                    // 获取ChartConfigs对象
                    ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
                    if (ObjectUtils.isEmpty(configs)) {
                        log.error("未找到图表配置。配置ID：{}", chartDateTto.getConfigId());
                        return AjaxResult.error("未找到该图表的配置，请检查配置ID是否正确。");
                    }

                    // 创建新的ChartSmartVo对象并设置属性
                    ChartSmartVo chartSmartVo = new ChartSmartVo();
                    chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
                    chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
                    chartSmartVo.setConfigValue(updatedChartValue);
                    chartSmartVo.setChartTypeId(configs.getChartTypeId());

                    newList.add(chartSmartVo);
                    System.out.println(updatedChartValue);
                    return AjaxResult.success(newList);

                } else if (chartDateTto.getConfigId() == 5 || chartDateTto.getConfigId() == 2) { //在以下修改
                    String chartValue = chartDateTto.getChartValue();
                    List<List<String>> lists = ChartDataUtils.extractDataFromList(chartDateTto.getDataList());
                    ObjectMapper objectMapper = new ObjectMapper();
                    JsonNode chartValueJson = objectMapper.readTree(chartValue);
                    // 获取dataset节点
                    ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");

                    System.out.println("我是dataset" + dataset);

                    for (List<String> list : lists) {
                        System.out.println("我是list" + list);
                    }
                    // 更新dataset中的source节点
                    dataset = ChartDataUtils.updateDatasetSourcesCyclic(dataset, lists, objectMapper);

                    // 移除集合中第一个对象的第一个元素
                    if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
                        lists.get(0).remove(0);
                    }


                    // 获取series字段并删除其余对象
                    ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");
                    if (seriesArray.size() > 1) {
                        while (seriesArray.size() > 1) {
                            seriesArray.remove(1);
                        }
                    }

                    // 计算第一个对象剩余元素数量
                    int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();

                    // 复制series的第一个对象
                    JsonNode firstSeriesObject = seriesArray.get(0);
                    for (int i = 1; i < firstObjectRemainingElements; i++) {
                        seriesArray.add(firstSeriesObject.deepCopy());
                    }

                    // 在这里添加新的逻辑来处理 datasetIndex 字段
                    for (int i = 0; i < seriesArray.size(); i++) {
                        ObjectNode seriesObject = (ObjectNode) seriesArray.get(i);
                        if (seriesObject.has("datasetIndex")) {
                            seriesObject.put("datasetIndex", i);
                        } else {
                            seriesObject.put("datasetIndex", i);
                        }
                    }

                    // 获取legend节点并更新data
                    JsonNode legend = chartValueJson.path("legend");
                    ChartDataUtils.updateLegendData(legend, lists);

                    // 更新series中每个对象的name字段
                    for (int i = 0; i < firstObjectRemainingElements; i++) {
                        ((ObjectNode) seriesArray.get(i)).put("name", lists.get(0).get(i));
                    }


                    // 处理颜色字段
                    ObjectNode jsonNode = (ObjectNode) objectMapper.readTree(chartValue);
                    // 获取 color 字段
                    JsonNode colorNode = jsonNode.path("color");
                    List<String> baseColors = new ArrayList<>();
                    // 遍历 color 数组并将每个颜色值添加到 baseColors 集合中
                    if (colorNode.isArray()) {
                        for (JsonNode color : colorNode) {
                            String colorValue = color.asText();
                            baseColors.add(colorValue);
                        }
                    }

                    int beforeSize = baseColors.size();
                    int afterSize = lists.get(0).size();
                    if (beforeSize < afterSize) {
                        chartValueJson = ColorUtils.processChartColorsHert(JSON.toJSONString(chartValueJson), afterSize);
                    }


                    String updatedChartValue = chartValueJson.toString();

                    ArrayList<ChartSmartVo> newList = new ArrayList<>();
                    // 获取ChartConfigs对象
                    ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
                    // 创建新的ChartSmartVo对象并设置属性
                    ChartSmartVo chartSmartVo = new ChartSmartVo();
                    chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
                    chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
                    chartSmartVo.setConfigValue(updatedChartValue);
                    chartSmartVo.setChartTypeId(configs.getChartTypeId());
                    newList.add(chartSmartVo);
                    System.out.println(updatedChartValue);
                    return AjaxResult.success(newList);

                } else if (chartDateTto.getConfigId() == 21) {

                    Sheet sheet = ExcelDataUtils.convertDataListToSheetsa(chartDateTto.getDataList());
                    String chartValue = chartDateTto.getChartValue();

                    //雷达图
                    ChartSmartVo chartV63 = getChartV29(ExcelDataUtils.getColData(sheet), chartValue);
                    ArrayList<ChartSmartVo> newList = new ArrayList<>();
                    newList.add(chartV63);
                    System.out.println(chartV63.getConfigValue().toString());

                    return AjaxResult.success(newList);
                } else if (chartDateTto.getConfigId() == 4) {

                    String chartValue = chartDateTto.getChartValue();
                    List<List<String>> lists = extractDataFromList(chartDateTto.getDataList());

                    System.out.println(chartValue);
                    ObjectMapper objectMapper = new ObjectMapper();
                    JsonNode chartValueJson = objectMapper.readTree(chartValue);

                    // 获取dataset节点并替换source节点
                    ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
                    for (JsonNode node : dataset) {
                        if (node.has("source")) {
                            ((ObjectNode) node).set("source", objectMapper.valueToTree(lists));
                            break;
                        }
                    }

                    // 移除集合中第一个对象的第一个元素
                    if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
                        lists.get(0).remove(0);
                    }
                    ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");

                    // 计算第一个对象元素数量
                    int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();

                    System.out.println(firstObjectRemainingElements);

                    // 如果series字段的对象数量小于第一个集合的数量，循环复制series中的对象使其数量相等
                    if (seriesArray.size() < firstObjectRemainingElements) {
                        JsonNode firstSeriesObject = seriesArray.get(0);
                        for (int i = seriesArray.size(); i < firstObjectRemainingElements; i++) {
                            seriesArray.add(firstSeriesObject.deepCopy());
                        }
                    } else {
                        // 删除多余的对象
                        while (seriesArray.size() > firstObjectRemainingElements) {
                            seriesArray.remove(seriesArray.size() - 1);
                        }
                    }

                    // 更新series中每个对象的name字段
                    for (int i = 0; i < firstObjectRemainingElements; i++) {
                        ((ObjectNode) seriesArray.get(i)).put("name", lists.get(0).get(i));
                    }

                    // 获取legend节点并更新data
                    JsonNode legend = chartValueJson.path("legend");
                    ChartDataUtils.updateLegendData(legend, lists);


                    // 处理颜色字段
                    ObjectNode jsonNode = (ObjectNode) objectMapper.readTree(chartValue);
                    // 获取 color 字段
                    JsonNode colorNode = jsonNode.path("color");

                    List<String> baseColors = new ArrayList<>();
                    // 遍历 color 数组并将每个颜色值添加到 baseColors 集合中
                    if (colorNode.isArray()) {
                        for (JsonNode color : colorNode) {
                            String colorValue = color.asText();
                            baseColors.add(colorValue);
                        }
                    }

                    int beforeSize = baseColors.size();
                    int afterSize = lists.get(0).size();
                    if (beforeSize < afterSize) {
                        chartValueJson = ColorUtils.processChartColorsHert(JSON.toJSONString(chartValueJson), afterSize);
                    }


                    String updatedChartValue = chartValueJson.toString();
                    ArrayList<ChartSmartVo> newList = new ArrayList<>();
                    // 获取ChartConfigs对象
                    ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
                    // 创建新的ChartSmartVo对象并设置属性
                    ChartSmartVo chartSmartVo = new ChartSmartVo();
                    chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
                    chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
                    chartSmartVo.setConfigValue(updatedChartValue);
                    chartSmartVo.setChartTypeId(configs.getChartTypeId());

                    newList.add(chartSmartVo);
                    System.out.println(updatedChartValue);
                    return AjaxResult.success(newList);
                } else if (chartDateTto.getConfigId() == 53) {

                    Sheet sheet = ExcelDataUtils.convertDataListToSheetsa(chartDateTto.getDataList());
                    String chartValue = chartDateTto.getChartValue();
                    //漏斗图
                    ChartSmartVo chartV263 = getChartV263(ExcelDataUtils.getData(sheet), chartValue);
                    ArrayList<ChartSmartVo> newList = new ArrayList<>();
                    newList.add(chartV263);
                    System.out.println(chartV263.getConfigValue().toString());
                    return AjaxResult.success(newList);

                } else if (chartDateTto.getConfigId() == 68) {

                    // 基础制图
                    Sheet sheet = ExcelDataUtils.convertDataListToSheetsa(chartDateTto.getDataList());
                    List<List<String>> getData = ExcelDataUtils.getData(sheet);

                    if (CollectionUtils.isEmpty(getData)) {
                        return AjaxResult.error("请填充数据");
                    }

                    // 提取并过滤第一行
                    List<String> firstName = new ArrayList<>();
                    List<String> nameList = getData.get(0);

                    for (String s : nameList) {
                        if (StringUtils.hasText(s)) { // 使用 StringUtils.hasText 代替 ObjectUtils.isNotEmpty(s) && !s.isEmpty()
                            firstName.add(s);
                        }
                    }

                    List<String> roomName = new ArrayList<>();
                    List<List<String>> list = new ArrayList<>();
                    list.add(firstName);

                    List<Map<String, Object>> datasets = new ArrayList<>();

                    for (int i = 1; i < getData.size(); i++) {
                        List<String> rowData = getData.get(i);
                        if (!CollectionUtils.isEmpty(rowData)) {
                            List<String> data = new ArrayList<>();
                            for (String s : rowData) {
                                if (StringUtils.hasText(s)) { // 使用 StringUtils.hasText 代替 ObjectUtils.isNotEmpty(s) && !s.isEmpty()
                                    data.add(s);
                                }
                            }

                            if (data.size() == 1) {
                                if (i != 1) {
                                    Map<String, Object> dataset = new HashMap<>();
                                    dataset.put("source", list);
                                    datasets.add(dataset);
                                    list = new ArrayList<>();
                                    list.add(firstName);
                                }
                                roomName.add(data.get(0));
                            } else {
                                list.add(data);
                                if (i == getData.size() - 1) {
                                    Map<String, Object> dataset = new HashMap<>();
                                    dataset.put("source", list);
                                    datasets.add(dataset);
                                }
                            }
                        }
                    }

                    // 解析图表值 JSON 并修改
                    JSONObject jsonObject = JSON.parseObject(chartDateTto.getChartValue());
                    List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
                    JSONObject seriesTemplate = series.get(0);
                    List<JSONObject> sDatas = new ArrayList<>();

                    if (roomName.isEmpty()) {
                        for (int i = 1; i < firstName.size(); i++) {
                            JSONObject object = new JSONObject();
                            object.putAll(seriesTemplate);
                            object.put("name", firstName.get(i));
                            sDatas.add(object);
                        }
                    } else {
                        for (int i = 0; i < roomName.size(); i++) {
                            for (int j = 1; j < firstName.size(); j++) {
                                JSONObject object = new JSONObject();
                                object.putAll(seriesTemplate);
                                object.put("stack", roomName.get(i));
                                object.put("datasetIndex", i);
                                object.put("name", firstName.get(j));
                                sDatas.add(object);
                            }
                        }
                    }

                    jsonObject.put("dataset", datasets);
                    jsonObject.put("series", sDatas);

                    // 处理 legend 作为 JSONArray
                    Object legendObject = jsonObject.get("legend");
                    if (legendObject instanceof JSONArray) {
                        JSONArray legendArray = (JSONArray) legendObject;
                        // 假设您想修改数组中的第一个对象
                        if (!legendArray.isEmpty()) {
                            JSONObject legend = legendArray.getJSONObject(0);
                            List<ChartLegendData> chartLegendData = new ArrayList<>();
                            for (int i = 1; i < firstName.size(); i++) {
                                ChartLegendData data = new ChartLegendData();
                                data.setName(firstName.get(i));
                                chartLegendData.add(data);
                            }
                            legend.put("data", chartLegendData);
                        }
                    } else if (legendObject instanceof JSONObject) {
                        JSONObject legend = (JSONObject) legendObject;
                        List<ChartLegendData> chartLegendData = new ArrayList<>();
                        for (int i = 1; i < firstName.size(); i++) {
                            ChartLegendData data = new ChartLegendData();
                            data.setName(firstName.get(i));
                            chartLegendData.add(data);
                        }
                        legend.put("data", chartLegendData);
                        jsonObject.put("legend", legend);
                    }

                    // 处理 series 的 itemStyle 的 barBorderWidth 和 barBorderColor
                    List<JSONObject> seriesList = (List<JSONObject>) jsonObject.get("series");

                    for (JSONObject seriesObj : seriesList) {
                        JSONObject itemStyle = seriesObj.getJSONObject("itemStyle");
                        if (itemStyle != null) {
                            // 删除 barBorderWidth 和 barBorderColor
                            itemStyle.remove("barBorderWidth");
                            itemStyle.remove("barBorderColor");
                        }
                    }

                    ArrayList<ChartSmartVo> newList = new ArrayList<>();
                    // 获取 ChartConfigs 对象
                    ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
                    // 创建新的 ChartSmartVo 对象并设置属性
                    ChartSmartVo chartSmartVo = new ChartSmartVo();
                    chartSmartVo.setConfigId(configs.getId()); // 假设 ChartConfigs 有 getId 方法
                    chartSmartVo.setConfigName(configs.getConfigName()); // 假设 ChartConfigs 有 getConfigName 方法
                    chartSmartVo.setConfigValue(JSON.toJSONString(jsonObject));
                    chartSmartVo.setChartTypeId(configs.getChartTypeId());

                    newList.add(chartSmartVo);
                    System.out.println(JSON.toJSONString(jsonObject));
                    return AjaxResult.success(newList);











                  /*  //基础制图
                    Sheet sheet = ExcelDataUtils.convertDataListToSheetsa(chartDateTto.getDataList());
                    List<List<String>> getData = ExcelDataUtils.getData(sheet);

                    if (CollectionUtils.isEmpty(getData)) {
                        return AjaxResult.error("请填充数据");
                    }

                    // Extract and filter the first row
                    List<String> firstName = new ArrayList<>();
                    List<String> nameList = getData.get(0);

                    for (String s : nameList) {
                        if (ObjectUtils.isNotEmpty(s) && !s.isEmpty()) {
                            firstName.add(s);
                        }
                    }

                    List<String> roomName = new ArrayList<>();
                    List<List<String>> list = new ArrayList<>();
                    list.add(firstName);

                    List<Map<String, Object>> datasets = new ArrayList<>();

                    for (int i = 1; i < getData.size(); i++) {
                        List<String> rowData = getData.get(i);
                        if (!CollectionUtils.isEmpty(rowData)) {
                            List<String> data = new ArrayList<>();
                            for (String s : rowData) {
                                if (ObjectUtils.isNotEmpty(s) && !s.isEmpty()) {
                                    data.add(s);
                                }
                            }

                            if (data.size() == 1) {
                                if (i != 1) {
                                    Map<String, Object> dataset = new HashMap<>();
                                    dataset.put("source", list);
                                    datasets.add(dataset);
                                    list = new ArrayList<>();
                                    list.add(firstName);
                                }
                                roomName.add(data.get(0));
                            } else {
                                list.add(data);
                                if (i == getData.size() - 1) {
                                    Map<String, Object> dataset = new HashMap<>();
                                    dataset.put("source", list);
                                    datasets.add(dataset);
                                }
                            }
                        }
                    }

                    // Parse chart value JSON and modify it
                    JSONObject jsonObject = JSON.parseObject(chartDateTto.getChartValue());
                    List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
                    JSONObject seriesTemplate = series.get(0);
                    List<JSONObject> sDatas = new ArrayList<>();

                    if (roomName.isEmpty()) {
                        for (int i = 1; i < firstName.size(); i++) {
                            JSONObject object = new JSONObject();
                            object.putAll(seriesTemplate);
                            object.put("name", firstName.get(i));
                            sDatas.add(object);
                        }
                    } else {
                        for (int i = 0; i < roomName.size(); i++) {
                            for (int j = 1; j < firstName.size(); j++) {
                                JSONObject object = new JSONObject();
                                object.putAll(seriesTemplate);
                                object.put("stack", roomName.get(i));
                                object.put("datasetIndex", i);
                                object.put("name", firstName.get(j));
                                sDatas.add(object);
                            }
                        }
                    }

                    jsonObject.put("dataset", datasets);
                    jsonObject.put("series", sDatas);

                    // Handle the legend as a JSONArray
                    Object legendObject = jsonObject.get("legend");
                    if (legendObject instanceof JSONArray) {
                        JSONArray legendArray = (JSONArray) legendObject;
                        // Assuming you want to modify the first object in the array
                        if (!legendArray.isEmpty()) {
                            JSONObject legend = legendArray.getJSONObject(0);
                            List<ChartLegendData> chartLegendData = new ArrayList<>();
                            for (int i = 1; i < firstName.size(); i++) {
                                ChartLegendData data = new ChartLegendData();
                                data.setName(firstName.get(i));
                                chartLegendData.add(data);
                            }
                            legend.put("data", chartLegendData);
                        }
                    } else if (legendObject instanceof JSONObject) {
                        JSONObject legend = (JSONObject) legendObject;
                        List<ChartLegendData> chartLegendData = new ArrayList<>();
                        for (int i = 1; i < firstName.size(); i++) {
                            ChartLegendData data = new ChartLegendData();
                            data.setName(firstName.get(i));
                            chartLegendData.add(data);
                        }
                        legend.put("data", chartLegendData);
                        jsonObject.put("legend", legend);
                    }


                    // series  itemStyle  的 barBorderWidth和 barBorderColor
                    // 获取series数组
                    List<JSONObject> seriesList = (List<JSONObject>) jsonObject.get("series");

                    // 遍历series数组
                    for (JSONObject seriesObj : seriesList) {
                        JSONObject itemStyle = seriesObj.getJSONObject("itemStyle");
                        if (itemStyle != null) {
                            // 删除 barBorderWidth 和 barBorderColor
                            itemStyle.remove("barBorderWidth");
                            itemStyle.remove("barBorderColor");
                        }
                    }

                    ArrayList<ChartSmartVo> newList = new ArrayList<>();
                    // 获取ChartConfigs对象
                    ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
                    // 创建新的ChartSmartVo对象并设置属性
                    ChartSmartVo chartSmartVo = new ChartSmartVo();
                    chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
                    chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
                    chartSmartVo.setConfigValue(JSON.toJSONString(jsonObject));
                    chartSmartVo.setChartTypeId(configs.getChartTypeId());

                    newList.add(chartSmartVo);
                    System.out.println(JSON.toJSONString(jsonObject));
                    return AjaxResult.success(newList);*/


                } else {
                    String chartValue = chartDateTto.getChartValue();
                    List<List<String>> lists = extractDataFromList(chartDateTto.getDataList());
                    System.out.println(chartValue);
                    ObjectMapper objectMapper = new ObjectMapper();
                    JsonNode chartValueJson = objectMapper.readTree(chartValue);


                    // 获取dataset节点并替换source节点
                    ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
                    for (JsonNode node : dataset) {
                        if (node.has("source")) {
                            ((ObjectNode) node).set("source", objectMapper.valueToTree(lists));
                            break;
                        }
                    }

                    // 移除集合中第一个对象的第一个元素
                    if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
                        lists.get(0).remove(0);
                    }
                    ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");

                    // 计算第一个对象元素数量
                    int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();

                    System.out.println(firstObjectRemainingElements);

                    // 如果series字段的对象数量小于第一个集合的数量，循环复制series中的对象使其数量相等
                    if (seriesArray.size() < firstObjectRemainingElements) {
                        JsonNode firstSeriesObject = seriesArray.get(0);
                        for (int i = seriesArray.size(); i < firstObjectRemainingElements; i++) {
                            seriesArray.add(firstSeriesObject.deepCopy());
                        }
                    } else {
                        // 删除多余的对象
                        while (seriesArray.size() > firstObjectRemainingElements) {
                            seriesArray.remove(seriesArray.size() - 1);
                        }
                    }

                    // 更新series中每个对象的name字段
                    for (int i = 0; i < firstObjectRemainingElements; i++) {
                        ((ObjectNode) seriesArray.get(i)).put("name", lists.get(0).get(i));
                    }

                    // 获取legend节点并更新data
                    JsonNode legend = chartValueJson.path("legend");
                    ChartDataUtils.updateLegendData(legend, lists);


                    // 处理颜色字段
                    ObjectNode jsonNode = (ObjectNode) objectMapper.readTree(chartValue);
                    // 获取 color 字段
                    JsonNode colorNode = jsonNode.path("color");
                    List<String> baseColors = new ArrayList<>();
                    // 遍历 color 数组并将每个颜色值添加到 baseColors 集合中
                    if (colorNode.isArray()) {
                        for (JsonNode color : colorNode) {
                            String colorValue = color.asText();
                            baseColors.add(colorValue);
                        }
                    }

                    int beforeSize = baseColors.size();
                    int afterSize = lists.get(0).size();
                    if (beforeSize < afterSize) {
                        chartValueJson = ColorUtils.processChartColorsHert(JSON.toJSONString(chartValueJson), afterSize);
                    }
                    String updatedChartValue = chartValueJson.toString();
                    ArrayList<ChartSmartVo> newList = new ArrayList<>();
                    // 获取ChartConfigs对象
                    ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
                    // 创建新的ChartSmartVo对象并设置属性
                    ChartSmartVo chartSmartVo = new ChartSmartVo();
                    chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
                    chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
                    chartSmartVo.setConfigValue(updatedChartValue);
                    chartSmartVo.setChartTypeId(configs.getChartTypeId());

                    newList.add(chartSmartVo);
                    System.out.println(updatedChartValue);
                    return AjaxResult.success(newList);
                }
            } else {
                if (chartDateTto.getConfigId() == 14 || chartDateTto.getConfigId() == 45) {
                    // chart_configs_interface的config_id 等于 chart_configs的id字段
                    Integer id = chartDateTto.getConfigId();
                    // 根据id查询chart_configs
                    ChartConfigs chartConfigs = chartConfigsService.getById(id);
                    if (ObjectUtils.isEmpty(chartConfigs)) {
                        log.error("未找到图表配置。配置ID：{}", chartDateTto.getConfigId());
                        return AjaxResult.error("未找到该图表的配置，请检查配置ID是否正确。");
                    }
                    // 模版
                    String configValue = chartConfigs.getConfigValue();

                    List<List<String>> lists = extractDataFromList(chartDateTto.getDataList());
                    ObjectMapper objectMapper = new ObjectMapper();
                    JsonNode chartValueJson = objectMapper.readTree(configValue);
                    JsonNode legend = chartValueJson.path("legend");
                    ChartDataUtils.updateLegendDataHert(legend, lists, objectMapper);

                    // 获取dataset节点并替换source节点
                    JsonNode datasetNode = chartValueJson.path("dataset");
                    if (datasetNode.isObject()) {
                        ObjectNode datasetObject = (ObjectNode) datasetNode;
                        if (datasetObject.has("source")) {
                            datasetObject.set("source", objectMapper.valueToTree(lists));
                        }
                    }

                    // 移除集合中第一个对象的第一个元素
                    if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
                        lists.get(0).remove(0);
                    }
                    ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");

                    // 计算第一个对象元素数量
                    int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();
                    System.out.println(firstObjectRemainingElements);
                    // 如果series字段的对象数量小于第一个集合的数量，循环复制series中的对象使其数量相等
                    if (seriesArray.size() < firstObjectRemainingElements) {
                        JsonNode firstSeriesObject = seriesArray.get(0);
                        for (int i = seriesArray.size(); i < firstObjectRemainingElements; i++) {
                            seriesArray.add(firstSeriesObject.deepCopy());
                        }
                    } else {
                        // 删除多余的对象
                        while (seriesArray.size() > firstObjectRemainingElements) {
                            seriesArray.remove(seriesArray.size() - 1);
                        }
                    }
                    // 更新series中每个对象的name字段
                    for (int i = 0; i < firstObjectRemainingElements; i++) {
                        ((ObjectNode) seriesArray.get(i)).put("name", lists.get(0).get(i));
                    }


                    String updatedChartValue = chartValueJson.toString();
                    ArrayList<ChartSmartVo> newList = new ArrayList<>();
                    // 获取ChartConfigs对象
                    ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
                    if (ObjectUtils.isEmpty(configs)) {
                        log.error("未找到图表配置。配置ID：{}", chartDateTto.getConfigId());
                        return AjaxResult.error("未找到该图表的配置，请检查配置ID是否正确。");
                    }
                    // 创建新的ChartSmartVo对象并设置属性
                    ChartSmartVo chartSmartVo = new ChartSmartVo();
                    chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
                    chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
                    chartSmartVo.setConfigValue(updatedChartValue);
                    chartSmartVo.setChartTypeId(configs.getChartTypeId());
                    newList.add(chartSmartVo);
                    return AjaxResult.success(newList);

                } else if (chartDateTto.getConfigId() == 51) {
                    // chart_configs_interface的config_id 等于 chart_configs的id字段
                    Integer id = chartDateTto.getConfigId();
                    // 根据id查询chart_configs
                    ChartConfigs chartConfigs = chartConfigsService.getById(id);
                    if (ObjectUtils.isEmpty(chartConfigs)) {
                        log.error("未找到图表配置。配置ID：{}", chartDateTto.getConfigId());
                        return AjaxResult.error("未找到该图表的配置，请检查配置ID是否正确。");
                    }
                    // 模版
                    String chartValue = chartConfigs.getConfigValue();

                    List<List<String>> lists = extractDataFromList(chartDateTto.getDataList());

                    ObjectMapper objectMapper = new ObjectMapper();
                    JsonNode chartValueJson = objectMapper.readTree(chartValue);
                    JsonNode legend = chartValueJson.path("legend");
                    ChartDataUtils.updateLegendDataHert(legend, lists, objectMapper);

                    // 获取dataset节点并替换source节点
                    JsonNode datasetNode = chartValueJson.path("dataset");
                    if (datasetNode.isObject() && datasetNode.has("source")) {
                        ((ObjectNode) datasetNode).set("source", objectMapper.valueToTree(lists));
                    }
                    // 移除集合中第一个对象的第一个元素
                    if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
                        lists.get(0).remove(0);
                    }
                    // 获取series字段并删除其余对象
                    ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");
                    if (seriesArray.size() > 1) {
                        while (seriesArray.size() > 1) {
                            seriesArray.remove(1);
                        }
                    }
                    // 计算第一个对象剩余元素数量
                    int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();
                    // 复制series的第一个对象
                    JsonNode firstSeriesObject = seriesArray.get(0);
                    for (int i = 1; i < firstObjectRemainingElements; i++) {
                        seriesArray.add(firstSeriesObject.deepCopy());
                    }
                    // 更新series中每个对象的name字段
                    for (int i = 0; i < firstObjectRemainingElements; i++) {
                        ObjectNode seriesObject = (ObjectNode) seriesArray.get(i);
                        seriesObject.put("name", lists.get(0).get(i));
                    }
                    // 删除每个series对象的step字段的值
                    for (int i = 0; i < seriesArray.size(); i++) {
                        ObjectNode seriesObject = (ObjectNode) seriesArray.get(i);
                        if (seriesObject.has("step")) {
                            seriesObject.remove("step");
                        }
                    }

                    // 对series中每个对象的step字段赋值
                    String[] stepValues = {"start", "middle", "end"};
                    for (int i = 0; i < seriesArray.size(); i++) {
                        ObjectNode seriesObject = (ObjectNode) seriesArray.get(i);
                        seriesObject.put("step", stepValues[i % stepValues.length]);
                    }

                    // 遍历series字段中的对象，判断是否有name字段，如果有则将其值赋给该对象的encode的y字段
                    for (int i = 0; i < seriesArray.size(); i++) {
                        ObjectNode seriesObject = (ObjectNode) seriesArray.get(i);
                        if (seriesObject.has("name")) {
                            String nameValue = seriesObject.get("name").asText();
                            if (!seriesObject.has("encode")) {
                                seriesObject.set("encode", objectMapper.createObjectNode());
                            }
                            ((ObjectNode) seriesObject.get("encode")).put("y", nameValue);
                        }
                    }

                    String updatedChartValue = chartValueJson.toString();
                    ArrayList<ChartSmartVo> newList = new ArrayList<>();
                    // 获取ChartConfigs对象
                    ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());

                    // 创建新的ChartSmartVo对象并设置属性
                    ChartSmartVo chartSmartVo = new ChartSmartVo();
                    chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
                    chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
                    chartSmartVo.setConfigValue(updatedChartValue);
                    chartSmartVo.setChartTypeId(configs.getChartTypeId());
                    newList.add(chartSmartVo);
                    System.out.println(updatedChartValue);
                    return AjaxResult.success(newList);
                } else if (chartDateTto.getConfigId() == 5) {
                    Integer id = chartDateTto.getConfigId();
                    // 根据id查询chart_configs
                    ChartConfigs chartConfigs = chartConfigsService.getById(id);
                    // 模版
                    String chartValue = chartConfigs.getConfigValue();
                    List<List<String>> lists = ChartDataUtils.extractDataFromList(chartDateTto.getDataList());
                    ObjectMapper objectMapper = new ObjectMapper();
                    JsonNode chartValueJson = objectMapper.readTree(chartValue);
                    // 获取dataset节点
                    ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");

                    // 更新dataset中的source节点
                    dataset = ChartDataUtils.updateDatasetSourcesCyclic(dataset, lists, objectMapper);
                    // 移除集合中第一个对象的第一个元素
                    if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
                        lists.get(0).remove(0);
                    }
                    // 获取series字段并删除其余对象
                    ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");
                    if (seriesArray.size() > 1) {
                        while (seriesArray.size() > 1) {
                            seriesArray.remove(1);
                        }
                    }
                    // 计算第一个对象剩余元素数量
                    int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();
                    // 复制series的第一个对象
                    JsonNode firstSeriesObject = seriesArray.get(0);
                    for (int i = 1; i < firstObjectRemainingElements; i++) {
                        seriesArray.add(firstSeriesObject.deepCopy());
                    }
                    // 在这里添加新的逻辑来处理 datasetIndex 字段
                    for (int i = 0; i < seriesArray.size(); i++) {
                        ObjectNode seriesObject = (ObjectNode) seriesArray.get(i);
                        if (seriesObject.has("datasetIndex")) {
                            seriesObject.put("datasetIndex", i);
                        } else {
                            seriesObject.put("datasetIndex", i);
                        }
                    }
                    // 更新series中每个对象的name字段
                    for (int i = 0; i < firstObjectRemainingElements; i++) {
                        ((ObjectNode) seriesArray.get(i)).put("name", lists.get(0).get(i));
                    }
                    // 获取legend节点并更新data
                    JsonNode legend = chartValueJson.path("legend");
                    int size = lists.size();
                    System.out.println("我是数量" + size);
                    List<String> strings = lists.get(0);
                    System.out.println("我是leng里面的图例" + strings);
                    ChartDataUtils.updateLegendDatas(legend, lists);
                    String updatedChartValue = chartValueJson.toString();
                    ArrayList<ChartSmartVo> newList = new ArrayList<>();
                    // 获取ChartConfigs对象
                    ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
                    //参数校验
                    // ChartValidatorUtils.ChartConfig(configs,chartDateTto.getConfigId());
                    // 创建新的ChartSmartVo对象并设置属性
                    ChartSmartVo chartSmartVo = new ChartSmartVo();
                    chartSmartVo.setConfigId(configs.getId());
                    chartSmartVo.setConfigName(configs.getConfigName());
                    chartSmartVo.setConfigValue(updatedChartValue);
                    chartSmartVo.setChartTypeId(configs.getChartTypeId());
                    newList.add(chartSmartVo);
                    return AjaxResult.success(newList);
                } else if (chartDateTto.getConfigId() == 68) {
                    Integer id = chartDateTto.getConfigId();
                    // 根据id查询chart_configs
                    ChartConfigs chartConfigs = chartConfigsService.getById(id);


                    // 模版
                    String chartValue = chartConfigs.getConfigValue();

                    // 基础制图
                    Sheet sheet = ExcelDataUtils.convertDataListToSheetsa(chartDateTto.getDataList());
                    List<List<String>> getData = ExcelDataUtils.getData(sheet);

                    if (CollectionUtils.isEmpty(getData)) {
                        return AjaxResult.error("请填充数据");
                    }

                    // 提取并过滤第一行
                    List<String> firstName = new ArrayList<>();
                    List<String> nameList = getData.get(0);

                    for (String s : nameList) {
                        if (StringUtils.hasText(s)) { // 使用 StringUtils.hasText 代替 ObjectUtils.isNotEmpty(s) && !s.isEmpty()
                            firstName.add(s);
                        }
                    }

                    List<String> roomName = new ArrayList<>();
                    List<List<String>> list = new ArrayList<>();
                    list.add(firstName);

                    List<Map<String, Object>> datasets = new ArrayList<>();

                    for (int i = 1; i < getData.size(); i++) {
                        List<String> rowData = getData.get(i);
                        if (!CollectionUtils.isEmpty(rowData)) {
                            List<String> data = new ArrayList<>();
                            for (String s : rowData) {
                                if (StringUtils.hasText(s)) { // 使用 StringUtils.hasText 代替 ObjectUtils.isNotEmpty(s) && !s.isEmpty()
                                    data.add(s);
                                }
                            }

                            if (data.size() == 1) {
                                if (i != 1) {
                                    Map<String, Object> dataset = new HashMap<>();
                                    dataset.put("source", list);
                                    datasets.add(dataset);
                                    list = new ArrayList<>();
                                    list.add(firstName);
                                }
                                roomName.add(data.get(0));
                            } else {
                                list.add(data);
                                if (i == getData.size() - 1) {
                                    Map<String, Object> dataset = new HashMap<>();
                                    dataset.put("source", list);
                                    datasets.add(dataset);
                                }
                            }
                        }
                    }

                    // 解析图表值 JSON 并修改
                    JSONObject jsonObject = JSON.parseObject(chartValue);
                    List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
                    JSONObject seriesTemplate = series.get(0);
                    List<JSONObject> sDatas = new ArrayList<>();

                    if (roomName.isEmpty()) {
                        for (int i = 1; i < firstName.size(); i++) {
                            JSONObject object = new JSONObject();
                            object.putAll(seriesTemplate);
                            object.put("name", firstName.get(i));
                            sDatas.add(object);
                        }
                    } else {
                        for (int i = 0; i < roomName.size(); i++) {
                            for (int j = 1; j < firstName.size(); j++) {
                                JSONObject object = new JSONObject();
                                object.putAll(seriesTemplate);
                                object.put("stack", roomName.get(i));
                                object.put("datasetIndex", i);
                                object.put("name", firstName.get(j));
                                sDatas.add(object);
                            }
                        }
                    }

                    jsonObject.put("dataset", datasets);
                    jsonObject.put("series", sDatas);

                    // 处理 legend 作为 JSONArray
                    Object legendObject = jsonObject.get("legend");
                    if (legendObject instanceof JSONArray) {
                        JSONArray legendArray = (JSONArray) legendObject;
                        // 假设您想修改数组中的第一个对象
                        if (!legendArray.isEmpty()) {
                            JSONObject legend = legendArray.getJSONObject(0);
                            List<ChartLegendData> chartLegendData = new ArrayList<>();
                            for (int i = 1; i < firstName.size(); i++) {
                                ChartLegendData data = new ChartLegendData();
                                data.setName(firstName.get(i));
                                chartLegendData.add(data);
                            }
                            legend.put("data", chartLegendData);
                        }
                    } else if (legendObject instanceof JSONObject) {
                        JSONObject legend = (JSONObject) legendObject;
                        List<ChartLegendData> chartLegendData = new ArrayList<>();
                        for (int i = 1; i < firstName.size(); i++) {
                            ChartLegendData data = new ChartLegendData();
                            data.setName(firstName.get(i));
                            chartLegendData.add(data);
                        }
                        legend.put("data", chartLegendData);
                        jsonObject.put("legend", legend);
                    }

                    // 处理 series 的 itemStyle 的 barBorderWidth 和 barBorderColor
                    List<JSONObject> seriesList = (List<JSONObject>) jsonObject.get("series");

                    for (JSONObject seriesObj : seriesList) {
                        JSONObject itemStyle = seriesObj.getJSONObject("itemStyle");
                        if (itemStyle != null) {
                            // 删除 barBorderWidth 和 barBorderColor
                            itemStyle.remove("barBorderWidth");
                            itemStyle.remove("barBorderColor");
                        }
                    }

                    ArrayList<ChartSmartVo> newList = new ArrayList<>();
                    // 获取 ChartConfigs 对象
                    ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
                    //参数校验

                    // 创建新的 ChartSmartVo 对象并设置属性
                    ChartSmartVo chartSmartVo = new ChartSmartVo();
                    chartSmartVo.setConfigId(configs.getId()); // 假设 ChartConfigs 有 getId 方法
                    chartSmartVo.setConfigName(configs.getConfigName()); // 假设 ChartConfigs 有 getConfigName 方法
                    chartSmartVo.setConfigValue(JSON.toJSONString(jsonObject));
                    chartSmartVo.setChartTypeId(configs.getChartTypeId());

                    newList.add(chartSmartVo);
                    System.out.println(JSON.toJSONString(jsonObject));
                    return AjaxResult.success(newList);

                }
                //chart_configs_interface的config_id 等于 chart_configs的id字段
                Integer id = chartDateTto.getConfigId();
                //根据id查询chart_configs
                ChartConfigs chartConfigs = chartConfigsService.getById(id);
                //参数校验

                //模版
                String configValue = chartConfigs.getConfigValue();

                List<List<String>> lists = extractDataFromList(chartDateTto.getDataList());
                ObjectMapper objectMapper = new ObjectMapper();
                JsonNode chartValueJson = objectMapper.readTree(configValue);
                JsonNode legend = chartValueJson.path("legend");
                ChartDataUtils.updateLegendDataHert(legend, lists, objectMapper);

                // 获取dataset节点并替换source节点
                ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
                for (JsonNode node : dataset) {
                    if (node.has("source")) {
                        ((ObjectNode) node).set("source", objectMapper.valueToTree(lists));
                        break;
                    }
                }
                // 移除集合中第一个对象的第一个元素
                if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
                    lists.get(0).remove(0);
                }
                ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");

                // 计算第一个对象元素数量
                int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();
                // 如果series字段的对象数量小于第一个集合的数量，循环复制series中的对象使其数量相等
                if (seriesArray.size() < firstObjectRemainingElements) {
                    JsonNode firstSeriesObject = seriesArray.get(0);
                    for (int i = seriesArray.size(); i < firstObjectRemainingElements; i++) {
                        seriesArray.add(firstSeriesObject.deepCopy());
                    }
                } else {
                    // 删除多余的对象
                    while (seriesArray.size() > firstObjectRemainingElements) {
                        seriesArray.remove(seriesArray.size() - 1);
                    }
                }

                // 更新series中每个对象的name字段
                for (int i = 0; i < firstObjectRemainingElements; i++) {
                    ((ObjectNode) seriesArray.get(i)).put("name", lists.get(0).get(i));
                }
                String updatedChartValue = chartValueJson.toString();
                ArrayList<ChartSmartVo> newList = new ArrayList<>();
                // 获取ChartConfigs对象
                ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
                //参数校验

                // 创建新的ChartSmartVo对象并设置属性
                ChartSmartVo chartSmartVo = new ChartSmartVo();
                chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
                chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
                chartSmartVo.setConfigValue(updatedChartValue);
                chartSmartVo.setChartTypeId(configs.getChartTypeId());
                newList.add(chartSmartVo);
                System.out.println(updatedChartValue);
                return AjaxResult.success(newList);
            }
        } catch (Exception e) {
            log.error("mart/chart/importData2接口发生异常configId={}", chartDateTto.getConfigId());
            throw new RuntimeException(e);
        }

    }


    private ChartSmartVo getChartV263(List<List<String>> getdata, String chartValue) throws Exception {
        List<String> xlist = new ArrayList<>();
        List<String> firstName = new ArrayList<>();
        List<List<Double>> dataList = new ArrayList<>();

        // 遍历输入数据并计算数据列表和X轴标签
        for (int i = 0; i < getdata.size(); i++) {
            List<String> rowData = getdata.get(i);
            List<String> newList = new ArrayList<>();
            double snum = 0;

            for (int i1 = 0; i1 < rowData.size(); i1++) {
                String s = rowData.get(i1);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    newList.add(s);
                    if (i != 0 && i1 != 0) {
                        snum += Double.parseDouble(s);
                    }
                }
            }

            if (!CollectionUtils.isEmpty(newList)) {
                if (i == 0) {
                    firstName.addAll(newList);
                } else {
                    List<Double> doubles = new ArrayList<>();
                    for (int i1 = 0; i1 < newList.size(); i1++) {
                        String s = newList.get(i1);
                        if (i1 == 0) {
                            xlist.add(s);
                        } else {
                            double v = Double.parseDouble(s);
                            doubles.add(v / snum);
                        }
                    }
                    if (!CollectionUtils.isEmpty(doubles)) {
                        dataList.add(doubles);
                    }
                }
            }
        }

        if (!CollectionUtils.isEmpty(firstName)) {
            firstName.remove(0);
        }

        List<List<Double>> datass = new ArrayList<>();
        for (int i = 0; i < firstName.size(); i++) {
            datass.add(new ArrayList<>());
        }

        for (List<Double> doubles : dataList) {
            for (int i1 = 0; i1 < doubles.size(); i1++) {
                datass.get(i1).add(doubles.get(i1));
            }
        }

        // 处理颜色字段
        ObjectMapper objectMapper = new ObjectMapper();
        ObjectNode jsonNode = (ObjectNode) objectMapper.readTree(chartValue);
        JsonNode colorNode = jsonNode.path("color");
        List<String> baseColors = new ArrayList<>();

        if (colorNode.isArray()) {
            for (JsonNode color : colorNode) {
                String colorValue = color.asText();
                baseColors.add(colorValue);
            }
        }

        int beforeSize = baseColors.size();
        int afterSize = datass.size();
        if (beforeSize < afterSize) {
            chartValue = String.valueOf(ColorUtils.processChartColorsHert(JSON.toJSONString(jsonNode), afterSize));
        }

        JSONObject jsonObject = JSON.parseObject(chartValue);

        // 更新X轴数据
        JSONArray xAxisArray = jsonObject.getJSONArray("xAxis");
        if (xAxisArray != null && !xAxisArray.isEmpty()) {
            JSONObject xAxis = xAxisArray.getJSONObject(0);
            xAxis.put("data", xlist);
            jsonObject.put("xAxis", xAxisArray);
        }

        // 处理 legend 中的 data 字段
        JSONArray legendArray = jsonObject.getJSONArray("legend");
        if (legendArray != null && !legendArray.isEmpty()) {
            for (int i = 0; i < legendArray.size(); i++) {
                JSONObject legendObject = legendArray.getJSONObject(i);
                JSONArray data = legendObject.getJSONArray("data");
                if (data != null && !data.isEmpty()) {
                    JSONArray newData = new JSONArray();
                    for (int j = 0; j < firstName.size(); j++) {
                        JSONObject legendItem = new JSONObject();
                        legendItem.put("name", firstName.get(j));  // 只修改 name 字段
                        newData.add(legendItem);
                    }
                    legendObject.put("data", newData);  // 更新 legend 中的 data
                }
            }
            jsonObject.put("legend", legendArray);
        }

        // 处理 series 部分
        List<JSONObject> newList = new ArrayList<>();
        JSONArray seriesArray = jsonObject.getJSONArray("series");
        if (seriesArray != null && !seriesArray.isEmpty()) {
            JSONObject jsonObject1 = seriesArray.getJSONObject(0);
            for (int i = 0; i < firstName.size(); i++) {
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.putAll(jsonObject1);
                jsonObject2.put("name", firstName.get(i));
                jsonObject2.put("data", datass.get(i));
                newList.add(jsonObject2);
            }
        }

        jsonObject.put("series", newList);

        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigValue(JSON.toJSONString(jsonObject));
        return chartSmartVo;
    }




/*   好用 private ChartSmartVo getChartV31(List<List<String>> getdata, Long configId) {

        String minV = getdata.get(0).get(1);
        String maxV = getdata.get(1).get(1);
        getdata.remove(0);
        getdata.remove(0);


        List<String> xList = new ArrayList<>();
        List<String> yList = new ArrayList<>();
        List<List<String>> dataList = new ArrayList<>();
        for (int i = 0; i < getdata.size(); i++) {
            if (!ObjectUtils.isEmpty(getdata.get(i)) && !getdata.get(i).equals("")) {
                if (i == 0) {
                    List<String> list = getdata.get(i);
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        if (!ObjectUtils.isEmpty(list.get(i1)) && !list.get(i).equals("")) {
                            xList.add(list.get(i1));
                        }
                    }
                } else {
                    String y = getdata.get(i).get(0);
                    if (!ObjectUtils.isEmpty(y) && !y.equals("")) {
                        yList.add(y);
                    }
                    getdata.get(i).remove(0);
                    dataList.add(getdata.get(i));
                }
            }
        }

        List<List<Object>> newDataList = new ArrayList<>();
        xList.remove(0);
        for (int i = 0; i < xList.size(); i++) {
            String x = xList.get(i);
            for (int j = 0; j < yList.size(); j++) {
                String s = dataList.get(j).get(i);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    String y = yList.get(j);
                    List<Object> cellList = new ArrayList<>();
                    cellList.add(x);
                    cellList.add(y);
                    cellList.add(Double.parseDouble(s));
                    newDataList.add(cellList);
                }
            }
        }

        ChartConfigs configs = chartConfigsService.getById(configId);
        JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());

        String minMaxAxis = JSON.toJSONString(jsonObject.get("visualMap"));
        JSONObject jsonObjectMinMAx = JSON.parseObject(minMaxAxis);
        jsonObjectMinMAx.put("min", Double.parseDouble(minV));
        jsonObjectMinMAx.put("max", Double.parseDouble(maxV));
        jsonObject.put("visualMap", jsonObjectMinMAx);

        String dataAxis = JSON.toJSONString(jsonObject.get("series"));
        JSONObject jsonObjectData = JSON.parseObject(dataAxis);
        jsonObjectData.put("data", newDataList);
        jsonObject.put("series", jsonObjectData);


        String xAxis = JSON.toJSONString(jsonObject.get("xAxis"));
        JSONObject jsonObjectX = JSON.parseObject(xAxis);
        jsonObjectX.put("data", xList);
        jsonObject.put("xAxis", jsonObjectX);

        String yAxis = JSON.toJSONString(jsonObject.get("yAxis"));
        JSONObject jsonObjectY = JSON.parseObject(yAxis);
        jsonObjectY.put("data", yList);
        jsonObject.put("yAxis", jsonObjectY);

        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigId(configs.getId());
        chartSmartVo.setConfigName(configs.getConfigName());
        chartSmartVo.setConfigValue(JSON.toJSONString(jsonObject));
        chartSmartVo.setChartTypeId(configs.getChartTypeId());
        return chartSmartVo;
    }

    @ApiOperation("彩色像素热力图 3d折线图 对角线相关矩阵 具有不同点大小和色调的散点图 关节和边缘直方图 热力图 - 大数据量")
    @PostMapping("importData3")
    @Transactional(rollbackFor = Exception.class) // 添加事务管理注解，捕获所有异常回滚事务
    public AjaxResult importData3(@RequestBody ChartDateTto chartDateTto) throws Exception {
        String chartValue = chartDateTto.getChartValue();
        Integer configId = chartDateTto.getConfigId();
        List<Map<String, Object>> rawDataList = chartDateTto.getDataList();
        List<List<String>> listList = extractDataFromList(rawDataList);

        if (CollectionUtils.isEmpty(listList)) {
            return AjaxResult.error("请填充数据");
        }

        if (ObjectUtils.isEmpty(listList.get(0).get(1))) {
            return AjaxResult.error("请输入最小值");
        }
        if (ObjectUtils.isEmpty(listList.get(1).get(1))) {
            return AjaxResult.error("请输入最大值");
        }
        ChartSmartVo chartV31 = getChartV31(listList, Long.valueOf(configId));
        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        newList.add(chartV31);
        String configValue = chartV31.getConfigValue();
        System.out.println(configValue);
        return AjaxResult.success(newList);
    }*/








/*
勉强能用
    @ApiOperation("彩色像素热力图 3d折线图 对角线相关矩阵 具有不同点大小和色调的散点图 关节和边缘直方图 热力图 - 大数据量")
    @PostMapping("importData3")
    @Transactional(rollbackFor = Exception.class) // 添加事务管理注解，捕获所有异常回滚事务
    public AjaxResult importData3(@RequestBody ChartDateTto chartDateTto) throws Exception {
        String chartValue = chartDateTto.getChartValue();
        Integer configId = chartDateTto.getConfigId();
        List<Map<String, Object>> rawDataList = chartDateTto.getDataList();
        List<List<String>> listList = extractDataFromListaaa(rawDataList);

        if (CollectionUtils.isEmpty(listList)) {
            return AjaxResult.error("请填充数据");
        }

        if (ObjectUtils.isEmpty(listList.get(0).get(1))) {
            return AjaxResult.error("请输入最小值");
        }
        if (ObjectUtils.isEmpty(listList.get(1).get(1))) {
            return AjaxResult.error("请输入最大值");
        }

        // 将 getChartV31 方法内容嵌入此处
        String minV = listList.get(0).get(1);
        String maxV = listList.get(1).get(1);
        listList.remove(0);
        listList.remove(0);

        List<String> xList = new ArrayList<>();
        List<String> yList = new ArrayList<>();
        List<List<String>> dataList = new ArrayList<>();
        for (int i = 0; i < listList.size(); i++) {
            if (!ObjectUtils.isEmpty(listList.get(i)) && !listList.get(i).equals("")) {
                if (i == 0) {
                    List<String> list = listList.get(i);
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        if (!ObjectUtils.isEmpty(list.get(i1)) && !list.get(i1).equals("")) {
                            xList.add(list.get(i1));
                        }
                    }
                } else {
                    String y = listList.get(i).get(0);
                    if (!ObjectUtils.isEmpty(y) && !y.equals("")) {
                        yList.add(y);
                    }
                    listList.get(i).remove(0);
                    dataList.add(listList.get(i));
                }
            }
        }

        List<List<Object>> newDataList = transformData(xList, yList, dataList);

        ChartConfigs configs = chartConfigsService.getById(configId);
        JSONObject jsonObjecta = JSON.parseObject(configs.getConfigValue());

        System.out.println("数据库中的: " + jsonObjecta);

        JSONObject jsonObject;
        try {
            jsonObject = JSONObject.parseObject(chartValue);
        } catch (JSONException e) {
            return AjaxResult.error("传入的 chartValue 数据解析错误: " + e.getMessage());
        }

        System.out.println("传入的: " + jsonObject);

        // 处理 visualMap
        Object visualMapObject = jsonObject.get("visualMap");
        if (visualMapObject == null) {
            return AjaxResult.error("visualMap is null");
        }

        JSONArray visualMapArray;
        if (visualMapObject instanceof JSONArray) {
            visualMapArray = (JSONArray) visualMapObject;
        } else if (visualMapObject instanceof JSONObject) {
            visualMapArray = new JSONArray();
            visualMapArray.add(visualMapObject);
        } else {
            return AjaxResult.error("visualMap 数据格式错误");
        }

        // 假设处理第一个 visualMap 对象
        JSONObject jsonObjectMinMax = visualMapArray.size() > 0 ? visualMapArray.getJSONObject(0) : new JSONObject();
        jsonObjectMinMax.put("min", Double.parseDouble(minV));
        jsonObjectMinMax.put("max", Double.parseDouble(maxV));
        jsonObject.put("visualMap", visualMapArray);

        // 解析并更新 series 数据
        Object seriesObject = jsonObject.get("series");
        if (seriesObject instanceof JSONObject) {
            JSONObject jsonObjectData = (JSONObject) seriesObject;
            jsonObjectData.put("data", newDataList);
            jsonObject.put("series", jsonObjectData);
        } else if (seriesObject instanceof JSONArray) {
            JSONArray jsonArrayData = (JSONArray) seriesObject;
            if (jsonArrayData.size() > 0) {
                JSONObject jsonObjectData = jsonArrayData.getJSONObject(0);
                jsonObjectData.put("data", newDataList);
                jsonArrayData.set(0, jsonObjectData);
                jsonObject.put("series", jsonArrayData);
            } else {
                return AjaxResult.error("series 数组为空");
            }
        } else {
            return AjaxResult.error("series 数据格式错误");
        }

        // 更新 xAxis 数据
        Object xAxisObject = jsonObject.get("xAxis");
        if (xAxisObject instanceof JSONObject) {
            JSONObject jsonObjectX = (JSONObject) xAxisObject;
            jsonObjectX.put("data", xList);
            jsonObject.put("xAxis", jsonObjectX);
        } else if (xAxisObject instanceof JSONArray) {
            JSONArray jsonArrayX = (JSONArray) xAxisObject;
            if (jsonArrayX.size() > 0) {
                JSONObject jsonObjectX = jsonArrayX.getJSONObject(0);
                jsonObjectX.put("data", xList);
                jsonArrayX.set(0, jsonObjectX);
                jsonObject.put("xAxis", jsonArrayX);
            } else {
                return AjaxResult.error("xAxis 数组为空");
            }
        } else {
            return AjaxResult.error("xAxis 数据格式错误");
        }

        // 更新 yAxis 数据
        Object yAxisObject = jsonObject.get("yAxis");
        if (yAxisObject instanceof JSONObject) {
            JSONObject jsonObjectY = (JSONObject) yAxisObject;
            jsonObjectY.put("data", yList);
            jsonObject.put("yAxis", jsonObjectY);
        } else if (yAxisObject instanceof JSONArray) {
            JSONArray jsonArrayY = (JSONArray) yAxisObject;
            if (jsonArrayY.size() > 0) {
                JSONObject jsonObjectY = jsonArrayY.getJSONObject(0);
                jsonObjectY.put("data", yList);
                jsonArrayY.set(0, jsonObjectY);
                jsonObject.put("yAxis", jsonArrayY);
            } else {
                return AjaxResult.error("yAxis 数组为空");
            }
        } else {
            return AjaxResult.error("yAxis 数据格式错误");
        }

        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigId(Long.valueOf(configId));
        chartSmartVo.setConfigName("Chart Config Name"); // 可根据需要调整
        chartSmartVo.setConfigValue(JSON.toJSONString(jsonObject));
        chartSmartVo.setChartTypeId(12345L); // 可根据需要调整

        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        newList.add(chartSmartVo);

        return AjaxResult.success(newList);
    }









    private List<List<String>> extractDataFromListaaa(List<Map<String, Object>> dataList) {
        List<List<String>> extractedData = new ArrayList<>();
        for (Map<String, Object> row : dataList) {
            List<String> rowData = new ArrayList<>();
            for (Map.Entry<String, Object> entry : row.entrySet()) {
                rowData.add(entry.getValue().toString());
            }
            extractedData.add(rowData);
        }
        return extractedData;
    }

    private List<String> extractXList(List<List<String>> listList) {
        List<String> xList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(listList)) {
            List<String> firstRow = listList.get(0);
            for (String cell : firstRow) {
                if (!ObjectUtils.isEmpty(cell)) {
                    xList.add(cell);
                }
            }
        }
        xList.remove(0); // 去除第一列（通常是Y轴标签）
        return xList;
    }

    private List<String> extractYList(List<List<String>> listList) {
        List<String> yList = new ArrayList<>();
        for (List<String> row : listList) {
            if (!ObjectUtils.isEmpty(row.get(0))) {
                yList.add(row.get(0));
            }
        }
        return yList;
    }

    private List<List<String>> extractDataList(List<List<String>> listList) {
        List<List<String>> dataList = new ArrayList<>();
        for (int i = 1; i < listList.size(); i++) {
            List<String> row = listList.get(i);
            row.remove(0); // 去除第一列（通常是Y轴标签）
            dataList.add(row);
        }
        return dataList;
    }


    private List<List<Object>> transformData(List<String> xList, List<String> yList, List<List<String>> dataList) {
        List<List<Object>> newDataList = new ArrayList<>();

        // 调试信息
        System.out.println("xList size: " + xList.size());
        System.out.println("yList size: " + yList.size());
        for (int i = 0; i < dataList.size(); i++) {
            System.out.println("dataList row " + i + " size: " + dataList.get(i).size());
        }

        for (int i = 0; i < xList.size(); i++) {
            String x = xList.get(i);
            for (int j = 0; j < yList.size(); j++) {
                if (j < dataList.size() && i < dataList.get(j).size()) {  // 边界检查
                    String s = dataList.get(j).get(i);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        String y = yList.get(j);
                        List<Object> cellList = new ArrayList<>();
                        cellList.add(x);
                        cellList.add(y);
                        cellList.add(Double.parseDouble(s));
                        newDataList.add(cellList);
                    }
                } else {
                    System.out.println("Skipping index [" + j + "][" + i + "] due to size mismatch");
                }
            }
        }
        return newDataList;
    }




    private void updateSeriesData(JSONObject jsonObject, List<List<Object>> newDataList) {
        String dataAxis = JSON.toJSONString(jsonObject.get("series"));
        JSONObject jsonObjectData = JSON.parseObject(dataAxis);
        jsonObjectData.put("data", newDataList);
        jsonObject.put("series", jsonObjectData);
    }

    private void updateAxisData(JSONObject jsonObject, String axisKey, List<String> axisData) {
        String axis = JSON.toJSONString(jsonObject.get(axisKey));
        JSONObject jsonObjectAxis = JSON.parseObject(axis);
        jsonObjectAxis.put("data", axisData);
        jsonObject.put(axisKey, jsonObjectAxis);
    }*/


    private ChartSmartVo getChartV31(List<List<String>> getdata, String updatedChartValue) {

        String minV = getdata.get(0).get(1);
        String maxV = getdata.get(1).get(1);
        getdata.remove(0);
        getdata.remove(0);


        List<String> xList = new ArrayList<>();
        List<String> yList = new ArrayList<>();
        List<List<String>> dataList = new ArrayList<>();
        for (int i = 0; i < getdata.size(); i++) {
            if (!ObjectUtils.isEmpty(getdata.get(i)) && !getdata.get(i).equals("")) {
                if (i == 0) {
                    List<String> list = getdata.get(i);
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        if (!ObjectUtils.isEmpty(list.get(i1)) && !list.get(i).equals("")) {
                            xList.add(list.get(i1));
                        }
                    }
                } else {
                    String y = getdata.get(i).get(0);
                    if (!ObjectUtils.isEmpty(y) && !y.equals("")) {
                        yList.add(y);
                    }
                    getdata.get(i).remove(0);
                    dataList.add(getdata.get(i));
                }
            }
        }

        List<List<Object>> newDataList = new ArrayList<>();
        xList.remove(0);
        for (int i = 0; i < xList.size(); i++) {
            String x = xList.get(i);
            for (int j = 0; j < yList.size(); j++) {
                String s = dataList.get(j).get(i);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    String y = yList.get(j);
                    List<Object> cellList = new ArrayList<>();
                    cellList.add(x);
                    cellList.add(y);
                    cellList.add(Double.parseDouble(s));
                    newDataList.add(cellList);
                }
            }
        }

        // ChartConfigs configs = chartConfigsService.getById(configId);
        JSONObject jsonObject = JSON.parseObject(updatedChartValue);

// 获取 visualMap 数组
        JSONArray visualMapArray = jsonObject.getJSONArray("visualMap");
// 获取 visualMap 的第一个对象
        JSONObject jsonObjectMinMax = visualMapArray.getJSONObject(0);
// 修改 min 和 max 值
        jsonObjectMinMax.put("min", Double.parseDouble(minV));
        jsonObjectMinMax.put("max", Double.parseDouble(maxV));
// 将修改后的对象放回 visualMap 数组
        visualMapArray.set(0, jsonObjectMinMax);
// 将 visualMap 数组放回 jsonObject
        jsonObject.put("visualMap", visualMapArray);


/*    String minMaxAxis = JSON.toJSONString(jsonObject.get("visualMap"));

    JSONObject jsonObjectMinMAx = JSON.parseObject(minMaxAxis);
    jsonObjectMinMAx.put("min",Double.parseDouble(minV));
    jsonObjectMinMAx.put("max",Double.parseDouble(maxV));
    jsonObject.put("visualMap",jsonObjectMinMAx);*/

        String dataAxis = JSON.toJSONString(jsonObject.get("series"));
        JSONObject jsonObjectData = JSON.parseObject(dataAxis);
        jsonObjectData.put("data", newDataList);
        jsonObject.put("series", jsonObjectData);


        String xAxis = JSON.toJSONString(jsonObject.get("xAxis"));
        JSONObject jsonObjectX = JSON.parseObject(xAxis);
        jsonObjectX.put("data", xList);
        jsonObject.put("xAxis", jsonObjectX);

        String yAxis = JSON.toJSONString(jsonObject.get("yAxis"));
        JSONObject jsonObjectY = JSON.parseObject(yAxis);
        jsonObjectY.put("data", yList);
        jsonObject.put("yAxis", jsonObjectY);

        ChartSmartVo chartSmartVo = new ChartSmartVo();
        //chartSmartVo.setConfigId(configs.getId());
        //chartSmartVo.setConfigName(configs.getConfigName());
        chartSmartVo.setConfigValue(JSON.toJSONString(jsonObject));
        //chartSmartVo.setChartTypeId(configs.getChartTypeId());
        return chartSmartVo;
    }

    private List<List<String>> extractDataFromListNullString(List<Map<String, Object>> dataList) {
        List<List<String>> extractedData = new ArrayList<>();

        for (Map<String, Object> row : dataList) {
            List<String> rowData = new ArrayList<>();
            for (Map.Entry<String, Object> entry : row.entrySet()) {
                Object value = entry.getValue();
                // 检查值是否为 null 或空字符串，并进行处理
                if (value == null || value.toString().isEmpty()) {
                    rowData.add("0"); // 为空或 null 时添加默认值 "0"
                } else {
                    rowData.add(value.toString());
                }
            }
            extractedData.add(rowData);
        }
        return extractedData;
    }

    private ChartSmartVo getChartV32(List<List<String>> getdata, String chartValue) {
        List<String> minList = getdata.get(0);
        List<String> maxList = getdata.get(1);

        String minV = minList.get(1);
        String maxV = maxList.get(1);

        List<String> xList = new ArrayList<>();
        List<String> yList = new ArrayList<>();

        List<List<String>> dataList = new ArrayList<>();
        for (int i = 2; i < getdata.size(); i++) {
            List<String> list = getdata.get(i);
            if (!CollectionUtils.isEmpty(list)) {
                List<String> newList = new ArrayList<>();
                for (int i1 = 0; i1 < list.size(); i1++) {
                    String s = list.get(i1);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        newList.add(s);
                    }
                }
                if (!CollectionUtils.isEmpty(newList)) {
                    if (i == 2) {
                        newList.remove(0);
                        yList.addAll(newList);
                    } else {
                        String x = newList.get(0);
                        if (!ObjectUtils.isEmpty(x) && !x.equals("")) {
                            xList.add(x);
                        }
                        newList.remove(0);
                        dataList.add(newList);
                    }
                }
            }
        }

        List<List<Object>> newDataList = new ArrayList<>();
        for (int i = 0; i < yList.size(); i++) {
            String y = yList.get(i);
            for (int j = 0; j < xList.size(); j++) {
                String x = xList.get(j);
                List<Object> cellList = new ArrayList<>();
                cellList.add(x);
                cellList.add(y);
                cellList.add(Double.parseDouble(dataList.get(j).get(i)));
                newDataList.add(cellList);
            }
        }
        JSONObject jsonObject = JSON.parseObject(chartValue);
        List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
        JSONObject jsonObject1 = series.get(0);
        jsonObject1.put("data", newDataList);
        series.remove(0);
        series.add(jsonObject1);
        jsonObject.put("series", series);

        Object visualMap = jsonObject.get("visualMap");
        if (visualMap instanceof JSONArray) {
            JSONArray visualMapArray = (JSONArray) visualMap;
            for (int i = 0; i < visualMapArray.size(); i++) {
                JSONObject minMaxAxis = visualMapArray.getJSONObject(i);
                minMaxAxis.put("min", Double.parseDouble(minV));
                minMaxAxis.put("max", Double.parseDouble(maxV));
            }
            jsonObject.put("visualMap", visualMapArray);
        } else if (visualMap instanceof JSONObject) {
            JSONObject minMaxAxis = (JSONObject) visualMap;
            minMaxAxis.put("min", Double.parseDouble(minV));
            minMaxAxis.put("max", Double.parseDouble(maxV));
            jsonObject.put("visualMap", minMaxAxis);
        }

        Object grid3D = jsonObject.get("grid3D");
        if (grid3D instanceof JSONObject) {
            JSONObject grid3DObject = (JSONObject) grid3D;
            if (xList.size() > 10 || yList.size() > 10) {
                grid3DObject.put("boxWidth", xList.size() * 8);
                grid3DObject.put("boxDepth", yList.size() * 4);
            } else if (xList.size() <= 10 && yList.size() <= 10) {
                grid3DObject.put("boxWidth", xList.size() * 20);
                grid3DObject.put("boxDepth", yList.size() * 10);
            }
            jsonObject.put("grid3D", grid3DObject);
        }

        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigValue(JSON.toJSONString(jsonObject));
        return chartSmartVo;
    }

    private static void saveSheetToFile(Sheet sheet, String fileName) {
        // 获取工作簿对象
        Workbook workbook = sheet.getWorkbook();

        try (FileOutputStream fileOut = new FileOutputStream(fileName)) {
            // 将工作簿写入文件输出流
            workbook.write(fileOut);
            System.out.println("Excel file has been generated successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static Sheet loadSheetFromFile(String filePath) {
        try (FileInputStream fileIn = new FileInputStream(new File(filePath))) {
            Workbook workbook = WorkbookFactory.create(fileIn);
            return workbook.getSheetAt(0);  // 假设只需要第一个 sheet
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    private ChartSmartVo getChartV31A(List<List<String>> getdata, String chartValue) {

        String minV = getdata.get(0).get(1);
        String maxV = getdata.get(1).get(1);
        getdata.remove(0);
        getdata.remove(0);


        List<String> xList = new ArrayList<>();
        List<String> yList = new ArrayList<>();
        List<List<String>> dataList = new ArrayList<>();
        for (int i = 0; i < getdata.size(); i++) {
            if (!ObjectUtils.isEmpty(getdata.get(i)) && !getdata.get(i).equals("")) {
                if (i == 0) {
                    List<String> list = getdata.get(i);
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        if (!ObjectUtils.isEmpty(list.get(i1)) && !list.get(i).equals("")) {
                            xList.add(list.get(i1));
                        }
                    }
                } else {
                    String y = getdata.get(i).get(0);
                    if (!ObjectUtils.isEmpty(y) && !y.equals("")) {
                        yList.add(y);
                    }
                    getdata.get(i).remove(0);
                    dataList.add(getdata.get(i));
                }
            }
        }

        List<List<Object>> newDataList = new ArrayList<>();
        xList.remove(0);
        for (int i = 0; i < xList.size(); i++) {
            String x = xList.get(i);
            for (int j = 0; j < yList.size(); j++) {
                String s = dataList.get(j).get(i);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    String y = yList.get(j);
                    List<Object> cellList = new ArrayList<>();
                    cellList.add(x);
                    cellList.add(y);
                    cellList.add(Double.parseDouble(s));
                    newDataList.add(cellList);
                }
            }
        }

        //ChartConfigs configs = chartConfigsService.getById(configId);
        JSONObject jsonObject = JSON.parseObject(chartValue);

        String minMaxAxis = JSON.toJSONString(jsonObject.get("visualMap"));
        JSONObject jsonObjectMinMAx = JSON.parseObject(minMaxAxis);
        jsonObjectMinMAx.put("min", Double.parseDouble(minV));
        jsonObjectMinMAx.put("max", Double.parseDouble(maxV));
        jsonObject.put("visualMap", jsonObjectMinMAx);


        String dataAxis = JSON.toJSONString(jsonObject.get("series"));
        JSONObject jsonObjectData = JSON.parseObject(dataAxis);
        jsonObjectData.put("data", newDataList);
        jsonObject.put("series", jsonObjectData);


        String xAxis = JSON.toJSONString(jsonObject.get("xAxis"));
        JSONObject jsonObjectX = JSON.parseObject(xAxis);
        jsonObjectX.put("data", xList);
        jsonObject.put("xAxis", jsonObjectX);

        String yAxis = JSON.toJSONString(jsonObject.get("yAxis"));
        JSONObject jsonObjectY = JSON.parseObject(yAxis);
        jsonObjectY.put("data", yList);
        jsonObject.put("yAxis", jsonObjectY);

        ChartSmartVo chartSmartVo = new ChartSmartVo();

        chartSmartVo.setConfigValue(JSON.toJSONString(jsonObject));
        return chartSmartVo;
    }


    private ChartSmartVo getChartV33(List<List<String>> getdata, String chartValue) {

        String minV = getdata.get(0).get(1);
        String maxV = getdata.get(1).get(1);
        getdata.remove(0);
        getdata.remove(0);


        List<String> xList = new ArrayList<>();
        List<String> yList = new ArrayList<>();
        List<List<String>> dataList = new ArrayList<>();
        for (int i = 0; i < getdata.size(); i++) {
            if (!ObjectUtils.isEmpty(getdata.get(i)) && !getdata.get(i).equals("")) {
                if (i == 0) {
                    List<String> list = getdata.get(i);
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        if (!ObjectUtils.isEmpty(list.get(i1)) && !list.get(i).equals("")) {
                            xList.add(list.get(i1));
                        }
                    }
                } else {
                    String y = getdata.get(i).get(0);
                    if (!ObjectUtils.isEmpty(y) && !y.equals("")) {
                        yList.add(y);
                    }
                    getdata.get(i).remove(0);
                    dataList.add(getdata.get(i));
                }
            }
        }

        List<List<Object>> newDataList = new ArrayList<>();
        xList.remove(0);
        for (int i = 0; i < xList.size(); i++) {
            String x = xList.get(i);
            for (int j = 0; j < yList.size() - i; j++) {
                String s = dataList.get(j).get(i);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    String y = yList.get(j);
                    List<Object> cellList = new ArrayList<>();
                    cellList.add(x);
                    cellList.add(y);
                    cellList.add(Double.parseDouble(s));
                    newDataList.add(cellList);
                }
            }
        }

        // ChartConfigs configs = chartConfigsService.getById(configId);
        JSONObject jsonObject = JSON.parseObject(chartValue);

        String minMaxAxis = JSON.toJSONString(jsonObject.get("visualMap"));
        JSONObject jsonObjectMinMAx = JSON.parseObject(minMaxAxis);
        jsonObjectMinMAx.put("min", Double.parseDouble(minV));
        jsonObjectMinMAx.put("max", Double.parseDouble(maxV));
        jsonObject.put("visualMap", jsonObjectMinMAx);


        String dataAxis = JSON.toJSONString(jsonObject.get("series"));
        JSONObject jsonObjectData = JSON.parseObject(dataAxis);
        jsonObjectData.put("data", newDataList);
        jsonObject.put("series", jsonObjectData);


        String xAxis = JSON.toJSONString(jsonObject.get("xAxis"));
        JSONObject jsonObjectX = JSON.parseObject(xAxis);
        jsonObjectX.put("data", xList);
        jsonObject.put("xAxis", jsonObjectX);

        String yAxis = JSON.toJSONString(jsonObject.get("yAxis"));
        JSONObject jsonObjectY = JSON.parseObject(yAxis);
        jsonObjectY.put("data", yList);
        jsonObject.put("yAxis", jsonObjectY);
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigValue(JSON.toJSONString(jsonObject));
        return chartSmartVo;
    }


//    @ApiOperation("具有不同点大小和色调的散点图老代码")
//    @PostMapping("importData9")
//    public AjaxResult importData310(@RequestBody ChartDateTto chartDateTto) {
//        // 参数校验
//        String validationMessage = ChartValidator.check(chartDateTto);
//        if (validationMessage != null) {
//            // 返回校验失败的提示信息给前端
//            return AjaxResult.error(validationMessage);
//        }
//        try {
//            // 提取数据
//            List<List<String>> lists = ChartDataProcessorUtils.extractDataFromList(chartDateTto.getDataList());
//            for (List<String> list : lists) {
//                System.out.println("我是configId等于18的数据" + list);
//            }
//            // 初始化 ObjectMapper
//            ObjectMapper mapper = new ObjectMapper();
//            JsonNode jsonNode;
//            try {
//                // 解析现有的 JSON 数据
//                jsonNode = mapper.readTree(chartDateTto.getChartValue());
//            } catch (JsonProcessingException e) {
//                System.err.println("JSON解析失败: " + e.getMessage());
//                return AjaxResult.error("JSON解析失败: " + e.getMessage());
//            }
//
//            ArrayNode datasetArray = (ArrayNode) jsonNode.path("dataset");
//            // 定义标题行
//            List<Object> biao = Arrays.asList("X", "Y", "Z");
//
//            // 获取图表的名称
//            String name = "";
//            if (lists != null && !lists.isEmpty()) {
//                List<String> firstList = lists.get(0);
//                if (firstList.size() >= 2) {
//                    name = firstList.get(1);
//                    System.out.println("我是图的名称：" + name);
//                } else {
//                    System.out.println("第一行数据不足 2 个元素，无法提取名称。");
//                }
//            } else {
//                System.out.println("数据列表为空，无法填充 source 数组。");
//            }
//
//            ArrayNode newSourceArray = mapper.createArrayNode();
//            ArrayNode headerRow = mapper.createArrayNode();
//            for (Object obj : biao) {
//                headerRow.add(obj.toString());
//            }
//            newSourceArray.add(headerRow);
//
//            // 遍历 lists，从第二行开始添加数据
//            if (lists != null && lists.size() > 1) {
//                for (int i = 1; i < lists.size(); i++) {
//                    List<String> currentList = lists.get(i);
//                    if (currentList.size() >= 2) {
//                        String date = currentList.get(0);
//                        String valueStr = currentList.get(1);
//                        double value;
//                        try {
//                            value = Double.parseDouble(valueStr);
//                        } catch (NumberFormatException e) {
//                            System.out.println("无法解析数值: " + valueStr + "，将跳过此条数据。");
//                            continue;
//                        }
//                        // 创建数据行
//                        ArrayNode dataRow = mapper.createArrayNode();
//                        dataRow.add(name);    // 第一列：充电桩名称
//                        dataRow.add(date);    // 第二列：日期
//                        dataRow.add(value);   // 第三列：数值
//
//                        // 添加到 newSourceArray
//                        newSourceArray.add(dataRow);
//                    } else {
//                        System.out.println("列表元素不足 2 个，跳过此条数据: " + currentList);
//                    }
//                }
//            } else {
//                System.out.println("没有足够的数据行来填充 source 数组。");
//            }
//
//            // 更新 dataset 中的 source 数组
//            for (JsonNode node : datasetArray) {
//                if (node.isObject()) {
//                    ObjectNode datasetObject = (ObjectNode) node;
//                    datasetObject.set("source", newSourceArray);
//                }
//            }
//
//            // 打印处理后的 JSON
//            String updatedJson;
//            try {
//                updatedJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode);
//            } catch (JsonProcessingException e) {
//                System.err.println("JSON生成失败: " + e.getMessage());
//                return AjaxResult.error("JSON生成失败: " + e.getMessage());
//            }
//            // 获取最小值和最大值
//            double[] minMaxValues = ChartDataProcessorUtils.getMinMaxValues(lists);
//            double minVs = minMaxValues[0];
//            double maxVs = minMaxValues[1];
//            String minV = String.valueOf(minVs);
//            String maxV = String.valueOf(maxVs);
//            System.out.println("最小值" + minV);
//            System.out.println("最大值" + maxV);
//
//            // 提取 visualMap 数组
//            ArrayNode visualMapArray = (ArrayNode) jsonNode.path("visualMap");
//            if (visualMapArray.size() > 0) {  // 确保 visualMap 数组不为空
//                for (int i = 0; i < visualMapArray.size(); i++) {
//                    ObjectNode visualMapObject = (ObjectNode) visualMapArray.get(i);
//                    visualMapObject.put("min", Double.parseDouble(minV));  // 更新 min
//                    visualMapObject.put("max", Double.parseDouble(maxV));  // 更新 max
//                    System.out.println("visualMap 更新后的值: " + visualMapObject);
//                    // 更新 range 数组
//                    ArrayNode rangeArray = (ArrayNode) visualMapObject.path("range");
//                    if (rangeArray.size() >= 2) {
//                        rangeArray.set(0, new TextNode(minV));
//                        rangeArray.set(1, new TextNode(maxV));
//                    } else {
//                        System.out.println("range 数组元素不足，无法更新。");
//                    }
//                }
//            } else {
//                System.out.println("visualMap 数组为空，无法更新 min 和 max。");
//            }
//            // 打印最终更新后的 JSON
//            try {
//                updatedJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode);
//            } catch (JsonProcessingException e) {
//                System.err.println("最终JSON生成失败: " + e.getMessage());
//                return AjaxResult.error("最终JSON生成失败: " + e.getMessage());
//            }
//            ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
//            ChartSmartVo chartSmartVo = new ChartSmartVo();
//            chartSmartVo.setConfigId(configs.getId());
//            chartSmartVo.setConfigName(configs.getConfigName());
//            chartSmartVo.setConfigValue(updatedJson);
//            chartSmartVo.setChartTypeId(configs.getChartTypeId());
//
//            ArrayList<Object> newList = new ArrayList<>();
//            newList.add(chartSmartVo);
//            return AjaxResult.success(newList);
//        } catch (NumberFormatException e) {
//            log.error("具有不同点大小和色调的散点图importData9,configId={}发生异常", chartDateTto.getConfigId());
//            throw new RuntimeException(e);
//        }
//    }




    @ApiOperation("具有不同点大小和色调的散点图")
    @PostMapping("importData9")
    public AjaxResult importData310(@RequestBody ChartDateTto chartDateTto) {
        // 参数校验
        String validationMessage = ChartValidator.check(chartDateTto);
        if (validationMessage != null) {
            // 返回校验失败的提示信息给前端
            return AjaxResult.error(validationMessage);
        }
        try {
            // 提取数据
            List<List<String>> lists = ChartDataProcessorUtils.extractDataFromList(chartDateTto.getDataList());
            // 初始化 ObjectMapper
            ObjectMapper mapper = new ObjectMapper();
            JsonNode jsonNode;
            try {
                // 解析现有的 JSON 数据
                jsonNode = mapper.readTree(chartDateTto.getChartValue());
            } catch (JsonProcessingException e) {
                System.err.println("JSON解析失败: " + e.getMessage());
                return AjaxResult.error("JSON解析失败: " + e.getMessage());
            }

            ArrayNode datasetArray = (ArrayNode) jsonNode.path("dataset");
            // 定义标题行
            List<Object> biao = Arrays.asList("X", "Y", "Z");

            // 获取图表的名称
            String name = "";
            if (lists != null && !lists.isEmpty()) {
                List<String> firstList = lists.get(0);
                if (firstList.size() >= 2) {
                    name = firstList.get(1);
                    System.out.println("我是图的名称：" + name);
                } else {
                    System.out.println("第一行数据不足 2 个元素，无法提取名称。");
                }
            } else {
                System.out.println("数据列表为空，无法填充 source 数组。");
            }

            ArrayNode newSourceArray = mapper.createArrayNode();
            ArrayNode headerRow = mapper.createArrayNode();
            for (Object obj : biao) {
                headerRow.add(obj.toString());
            }
            newSourceArray.add(headerRow);

            // 遍历 lists，从第二行开始添加数据
            if (lists != null && lists.size() > 1) {
                for (int i = 1; i < lists.size(); i++) {
                    List<String> currentList = lists.get(i);
                    if (currentList.size() >= 2) {
                        String date = currentList.get(0);
                        String valueStr = currentList.get(1);
                        double value;
                        try {
                            value = Double.parseDouble(valueStr);
                        } catch (NumberFormatException e) {
                            System.out.println("无法解析数值: " + valueStr + "，将跳过此条数据。");
                            continue;
                        }
                        // 创建数据行
                        ArrayNode dataRow = mapper.createArrayNode();
                        dataRow.add(name);    // 第一列：充电桩名称
                        dataRow.add(date);    // 第二列：日期
                        dataRow.add(value);   // 第三列：数值

                        // 添加到 newSourceArray
                        newSourceArray.add(dataRow);
                    } else {
                        System.out.println("列表元素不足 2 个，跳过此条数据: " + currentList);
                    }
                }
            } else {
                System.out.println("没有足够的数据行来填充 source 数组。");
            }

            // 更新 dataset 中的 source 数组
            for (JsonNode node : datasetArray) {
                if (node.isObject()) {
                    ObjectNode datasetObject = (ObjectNode) node;
                    datasetObject.set("source", newSourceArray);
                }
            }

            // 打印处理后的 JSON
            String updatedJson;
            try {
                updatedJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode);
            } catch (JsonProcessingException e) {
                System.err.println("JSON生成失败: " + e.getMessage());
                return AjaxResult.error("JSON生成失败: " + e.getMessage());
            }

            // 获取最小值和最大值
            double[] minMaxValues = ChartDataProcessorUtils.getMinMaxValues(lists);
            double minVs = minMaxValues[0];
            double maxVs = minMaxValues[1];
            String minV = String.valueOf(minVs);
            String maxV = String.valueOf(maxVs);
            System.out.println("最小值" + minV);
            System.out.println("最大值" + maxV);

            // 提取 visualMap 数组
            ArrayNode visualMapArray = (ArrayNode) jsonNode.path("visualMap");
            if (visualMapArray.size() > 0) {  // 确保 visualMap 数组不为空
                for (int i = 0; i < visualMapArray.size(); i++) {
                    ObjectNode visualMapObject = (ObjectNode) visualMapArray.get(i);
                    visualMapObject.put("min", Double.parseDouble(minV));  // 更新 min
                    visualMapObject.put("max", Double.parseDouble(maxV));  // 更新 max
                    System.out.println("visualMap 更新后的值: " + visualMapObject);
                    // 更新 range 数组
                    ArrayNode rangeArray = (ArrayNode) visualMapObject.path("range");
                    if (rangeArray.size() >= 2) {
                        rangeArray.set(0, new TextNode(minV));
                        rangeArray.set(1, new TextNode(maxV));
                    } else {
                        System.out.println("range 数组元素不足，无法更新。");
                    }
                }
            } else {
                System.out.println("visualMap 数组为空，无法更新 min 和 max。");
            }


            int afterSize = lists.get(0).size() - 2;
            System.out.println("我是要复制的次数" + afterSize);


            ArrayList<Object> arrayList = new ArrayList<>();

            // 从 lists 的第二个元素开始遍历
            for (int i = 1; i < lists.size(); i++) {
                List<String> list = lists.get(i);
                // 去掉 list 中的第一个元素
                List<String> sublist = list.subList(1, list.size());
                arrayList.add(sublist);
                System.out.println("我是configId等于18的数据" + sublist);
            }
            System.out.println("我是arrayList的数据" + arrayList);


            // 新的分组集合
            ArrayList<List<String>> dataLists = new ArrayList<>();
            // 确定最长的子列表的长度
            int maxLength = 0;
            for (Object obj : arrayList) {
                List<String> sublist = (List<String>) obj;
                if (sublist.size() > maxLength) {
                    maxLength = sublist.size();
                }
            }
            // 按照每个集合的第一个元素、第二个元素、第三个元素等进行分组
            for (int i = 0; i < maxLength; i++) {
                List<String> group = new ArrayList<>();
                for (Object obj : arrayList) {
                    List<String> sublist = (List<String>) obj;
                    if (i < sublist.size()) {
                        group.add(sublist.get(i));
                    }
                }
                dataLists.add(group);
            }


            List<String> nameList = lists.get(0).subList(1, lists.get(0).size());
            System.out.println("我是名称数据" + nameList);
            // 打印分组后的结果
            System.out.println("分组后的数据: " + dataLists);


            ArrayNode dataset = (ArrayNode) jsonNode.path("dataset");
            // 复制 series 数组中的第一个对象
            ArrayNode series = (ArrayNode) jsonNode.path("series");
         // 保留 dataset 数组中的第一个对象，删除其余对象
            if (dataset.size() > 1) {
                while (dataset.size() > 1) {
                    dataset.remove(1);  // 删除索引为 1 的元素（即第二个元素）
                }
            }

           // 保留 series 数组中的第一个对象，删除其余对象
            if (series.size() > 1) {
                while (series.size() > 1) {
                    series.remove(1);  // 删除索引为 1 的元素（即第二个元素）
                }
            }

            if (afterSize > 0) {
                // 复制前先保存原始的 dataset 对象
                ArrayNode originalDatasetArray = (ArrayNode) jsonNode.path("dataset");

                if (originalDatasetArray.size() > 0) {
                    // 取第一个对象作为模板
                    ObjectNode templateNode = (ObjectNode) originalDatasetArray.get(0);

                    // 开始复制
                    for (int i = 0; i < afterSize; i++) {
                        // 深拷贝 templateNode
                        ObjectNode newNode = mapper.createObjectNode();
                        Iterator<Map.Entry<String, JsonNode>> fields = templateNode.fields();
                        while (fields.hasNext()) {
                            Map.Entry<String, JsonNode> field = fields.next();
                            newNode.set(field.getKey(), field.getValue().deepCopy());
                        }

                        // 将新的节点添加到 dataset 中
                        originalDatasetArray.add(newNode);
                    }

                    // 更新每个新对象的 source 数组
                    for (int i = 0; i < Math.min(nameList.size(), originalDatasetArray.size()); i++) {
                        String names = nameList.get(i);  // 例如：充电桩1, 充电桩2
                        ObjectNode item = (ObjectNode) originalDatasetArray.get(i);
                        ArrayNode source = (ArrayNode) item.path("source");

                        // 更新 source 数组中的第一个元素（名称）
                        for (int j = 1; j < source.size(); j++) {
                            ArrayNode subArray = (ArrayNode) source.get(j);
                            if (subArray.size() > 0) {
                                subArray.set(0, mapper.valueToTree(names));  // 将名称填充到 source 的第一个位置
                            }
                        }
                    }

                    // 更新每个新对象的 source 数组的第三个值
                    // 更新每个新对象的 source 数组的第三个值
                    for (int i = 0; i < Math.min(dataLists.size(), originalDatasetArray.size()); i++) {
                        List<String> dataList = dataLists.get(i);  // 获取每个分组的数据，注意这是 List<String>
                        ObjectNode item = (ObjectNode) originalDatasetArray.get(i);
                        ArrayNode source = (ArrayNode) item.path("source");

                        // 更新 source 数组中的第三个值
                        for (int j = 1; j < source.size(); j++) {  // 从第二个元素开始
                            ArrayNode subArray = (ArrayNode) source.get(j);
                            if (subArray.size() > 2) {
                                // 将 String 转换为 Double
                                String strValue = dataList.get(j - 1);  // 获取对应的值（String 类型）
                                Double newValue = null;
                                try {
                                    newValue = Double.parseDouble(strValue);  // 将 String 转换为 Double
                                } catch (NumberFormatException e) {
                                    // 如果无法转换为 Double，可以在这里处理异常（例如设置为默认值或打印警告）
                                    System.out.println("无法将字符串转换为 Double: " + strValue);
                                }

                                // 更新第三个值
                                if (newValue != null) {
                                    subArray.set(2, mapper.valueToTree(newValue));  // 用新值替换原来的值
                                }
                            }
                        }
                    }

                } else {
                    System.out.println("原始 dataset 数组为空，无法进行复制。");
                }

                // 复制 series 数组中的第一个对象
                ArrayNode originalSeriesArray = (ArrayNode) jsonNode.path("series");

                if (originalSeriesArray.size() > 0) {
                    // 取第一个对象作为模板
                    ObjectNode seriesTemplateNode = (ObjectNode) originalSeriesArray.get(0);

                    // 开始复制
                    for (int i = 1; i <= afterSize; i++) { // 注意这里i从1开始，因为datasetIndex从1开始计数
                        // 深拷贝 seriesTemplateNode
                        ObjectNode newSeriesNode = mapper.createObjectNode();
                        Iterator<Map.Entry<String, JsonNode>> fields = seriesTemplateNode.fields();
                        while (fields.hasNext()) {
                            Map.Entry<String, JsonNode> field = fields.next();
                            newSeriesNode.set(field.getKey(), field.getValue().deepCopy());
                        }

                        // 添加 datasetIndex 字段并设置值
                        newSeriesNode.put("datasetIndex", i);

                        // 将新的节点添加到 series 中
                        originalSeriesArray.add(newSeriesNode);
                    }
                } else {
                    System.out.println("原始 series 数组为空，无法进行复制。");
                }
            } else {
                System.out.println("afterSize 不大于 0，无需进行复制。");
            }


//            if (afterSize > 0) {
//                // 复制前先保存原始的 dataset 对象
//                ArrayNode originalDatasetArray = (ArrayNode) jsonNode.path("dataset");
//
//                if (originalDatasetArray.size() > 0) {
//
//                    // 取第一个对象作为模板
//                    ObjectNode templateNode = (ObjectNode) originalDatasetArray.get(0);
//
//                    // 开始复制
//                    for (int i = 0; i < afterSize; i++) {
//                        // 深拷贝 templateNode
//                        ObjectNode newNode = mapper.createObjectNode();
//                        Iterator<Map.Entry<String, JsonNode>> fields = templateNode.fields();
//                        while (fields.hasNext()) {
//                            Map.Entry<String, JsonNode> field = fields.next();
//                            newNode.set(field.getKey(), field.getValue().deepCopy());
//                        }
//
//                        // 将新的节点添加到 dataset 中
//                        originalDatasetArray.add(newNode);
//                    }
//
//                    // 更新每个新对象的 source 数组
//                    for (int i = 0; i < Math.min(nameList.size(), originalDatasetArray.size()); i++) {
//                        String names = nameList.get(i);
//                        ObjectNode item = (ObjectNode) originalDatasetArray.get(i);
//                        ArrayNode source = (ArrayNode) item.path("source");
//
//                        // 更新 source 数组中的元素，除了第一个元素
//                        for (int j = 1; j < source.size(); j++) {
//                            ArrayNode subArray = (ArrayNode) source.get(j);
//                            if (subArray.size() > 0) {
//                                subArray.set(0, mapper.valueToTree(names));
//                            }
//                        }
//                    }
//
//                    // 更新每个新对象的 source 数组的第三个值
//                    for (int i = 0; i < Math.min(dataLists.size(), originalDatasetArray.size()); i++) {
//                        List<String> dataList = dataLists.get(i);
//                        ObjectNode item = (ObjectNode) originalDatasetArray.get(i);
//                        ArrayNode source = (ArrayNode) item.path("source");
//
//                        // 更新 source 数组中的元素，除了第一个元素
//                        for (int j = 1; j < Math.min(dataList.size(), source.size()); j++) {
//                            ArrayNode subArray = (ArrayNode) source.get(j);
//                            if (subArray.size() > 2) {
//                                subArray.set(2, mapper.valueToTree(dataList.get(j - 1)));
//                            }
//                        }
//                    }
//                } else {
//                    System.out.println("原始 dataset 数组为空，无法进行复制。");
//                }
//
//
//                // 复制 series 数组中的第一个对象
//                ArrayNode originalSeriesArray = (ArrayNode) jsonNode.path("series");
//
//                if (originalSeriesArray.size() > 0) {
//                    // 取第一个对象作为模板
//                    ObjectNode seriesTemplateNode = (ObjectNode) originalSeriesArray.get(0);
//
//                    // 开始复制
//                    for (int i = 1; i <= afterSize; i++) { // 注意这里i从1开始，因为datasetIndex从1开始计数
//                        // 深拷贝 seriesTemplateNode
//                        ObjectNode newSeriesNode = mapper.createObjectNode();
//                        Iterator<Map.Entry<String, JsonNode>> fields = seriesTemplateNode.fields();
//                        while (fields.hasNext()) {
//                            Map.Entry<String, JsonNode> field = fields.next();
//                            newSeriesNode.set(field.getKey(), field.getValue().deepCopy());
//                        }
//
//                        // 添加 datasetIndex 字段并设置值
//                        newSeriesNode.put("datasetIndex", i);
//
//                        // 将新的节点添加到 series 中
//                        originalSeriesArray.add(newSeriesNode);
//                    }
//                } else {
//                    System.out.println("原始 series 数组为空，无法进行复制。");
//                }
//
//                // 如果需要处理 dataset 相关逻辑，可以在此处添加
//                // 例如，更新每个新对象的 source 数组等操作
//                // 注意这里的逻辑没有改变，只是根据您的需求重点调整了 series 部分
//            } else {
//                System.out.println("afterSize 不大于 0，无需进行复制。");
//            }






            // 打印最终更新后的 JSON
            try {
                updatedJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode);
            } catch (JsonProcessingException e) {
                System.err.println("最终JSON生成失败: " + e.getMessage());
                return AjaxResult.error("最终JSON生成失败: " + e.getMessage());
            }
            ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
            ChartSmartVo chartSmartVo = new ChartSmartVo();
            chartSmartVo.setConfigId(configs.getId());
            chartSmartVo.setConfigName(configs.getConfigName());
            chartSmartVo.setConfigValue(updatedJson);
            chartSmartVo.setChartTypeId(configs.getChartTypeId());

            ArrayList<Object> newList = new ArrayList<>();
            newList.add(chartSmartVo);
            return AjaxResult.success(newList);
        } catch (NumberFormatException e) {
            log.error("具有不同点大小和色调的散点图importData9,configId={}发生异常", chartDateTto.getConfigId());
            throw new RuntimeException(e);
        }
    }


    @ApiOperation("关节和边缘直方图 ")
    @PostMapping("importData10")
    @Transactional(rollbackFor = Exception.class) // 添加事务管理注解，捕获所有异常回滚事务
    public AjaxResult importData10(@RequestBody ChartDateTto chartDateTto) throws Exception {
        // 参数校验
        String validationMessage = ChartValidator.check(chartDateTto);
        if (validationMessage != null) {
            // 返回校验失败的提示信息给前端
            return AjaxResult.error(validationMessage);
        }
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode chartValueJson = null;
            if (chartDateTto.getConfigId() == 19) {
                List<List<String>> getdata = ChartDataProcessorUtils.extractDataFromList(chartDateTto.getDataList());
                List<String> xList = new ArrayList<>();
                List<String> yList = new ArrayList<>();
                List<List<String>> dataList = new ArrayList<>();
                ChartDataProcessorUtils.processChartData(getdata, xList, yList, dataList);
                double[] minMaxValues = ChartDataProcessorUtils.getMinMaxValues(dataList);
                double minV = minMaxValues[0];
                double maxV = minMaxValues[1];
                List<List<Object>> newDataList = ChartDataProcessorUtils.getNewDataList(xList, yList, dataList);
                ChartDataProcessorUtils.updateChartConfigurations(chartValueJson, newDataList, minV, maxV, xList, yList, objectMapper);
                ChartSmartVo chartSmartVo = new ChartSmartVo();
                chartSmartVo.setConfigValue(objectMapper.writeValueAsString(chartValueJson));
                List<ChartSmartVo> newList = new ArrayList<>();
                newList.add(chartSmartVo);
                return AjaxResult.success(newList);
            }
        } catch (Exception e) {
            log.error("关节和边缘直方图importData10,configId={}发生异常", chartDateTto.getConfigId());
            throw new RuntimeException(e);
        }
        return AjaxResult.success("还在开发中");
    }


    @ApiOperation("  关节和边缘直方图 ")
    @PostMapping("importData11")
    @Transactional(rollbackFor = Exception.class) // 添加事务管理注解，捕获所有异常回滚事务
    public AjaxResult importData11(@RequestBody ChartDateTto chartDateTto) throws Exception {
        // 参数校验
        String validationMessage = ChartValidator.check(chartDateTto);
        if (validationMessage != null) {
            // 返回校验失败的提示信息给前端
            return AjaxResult.error(validationMessage);
        }

        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode chartValueJson;
            if (chartDateTto.getChartValue() != null) {
                chartValueJson = objectMapper.readTree(chartDateTto.getChartValue());
                if (chartDateTto.getConfigId() == 19) {
                    List<List<String>> getdata = ChartDataProcessorUtils.extractDataFromList(chartDateTto.getDataList());
                    List<String> xList = new ArrayList<>();
                    List<String> yList = new ArrayList<>();
                    List<List<String>> dataList = new ArrayList<>();
                    ChartDataProcessorUtils.processChartData(getdata, xList, yList, dataList);
                    double[] minMaxValues = ChartDataProcessorUtils.getMinMaxValues(dataList);
                    double minV = minMaxValues[0];
                    double maxV = minMaxValues[1];
                    List<List<Object>> newDataList = ChartDataProcessorUtils.getNewDataList(xList, yList, dataList);
                    ChartDataProcessorUtils.updateChartConfigurations(chartValueJson, newDataList, minV, maxV, xList, yList, objectMapper);
                    ChartSmartVo chartSmartVo = new ChartSmartVo();
                    chartSmartVo.setConfigValue(objectMapper.writeValueAsString(chartValueJson));
                    List<ChartSmartVo> newList = new ArrayList<>();
                    newList.add(chartSmartVo);

                    return AjaxResult.success(newList);
                }
            } else {
                // 如果 chartValue 为空，获取配置
                Integer id = chartDateTto.getConfigId();
                ChartConfigs chartConfigs = chartConfigsService.getById(id);
                chartValueJson = objectMapper.readTree(chartConfigs.getConfigValue());
                // 此处也需要处理 configId 为 19 的逻辑
                if (chartDateTto.getConfigId() == 19) {
                    List<List<String>> getdata = ChartDataProcessorUtils.extractDataFromList(chartDateTto.getDataList());
                    List<String> xList = new ArrayList<>();
                    List<String> yList = new ArrayList<>();
                    List<List<String>> dataList = new ArrayList<>();
                    ChartDataProcessorUtils.processChartData(getdata, xList, yList, dataList);
                    double[] minMaxValues = ChartDataProcessorUtils.getMinMaxValues(dataList);
                    double minV = minMaxValues[0];
                    double maxV = minMaxValues[1];
                    List<List<Object>> newDataList = ChartDataProcessorUtils.getNewDataList(xList, yList, dataList);

                    ChartDataProcessorUtils.updateChartConfigurations(chartValueJson, newDataList, minV, maxV, xList, yList, objectMapper);
                    ChartSmartVo chartSmartVo = new ChartSmartVo();
                    chartSmartVo.setConfigValue(objectMapper.writeValueAsString(chartValueJson));
                    List<ChartSmartVo> newList = new ArrayList<>();
                    newList.add(chartSmartVo);
                    return AjaxResult.success(newList);
                }
            }
        } catch (Exception e) {
            log.error("关节和边缘直方图importData11,configId={}发生异常", chartDateTto.getConfigId());
            throw new RuntimeException(e);
        }
        return AjaxResult.error("未处理的配置ID");
    }


    @ApiOperation(" 3d折线图 对角线相关矩阵  关节和边缘直方图 热力图 - 大数据量")//之前有彩色像素热力图
    @PostMapping("importData3")
    @Transactional(rollbackFor = Exception.class) // 添加事务管理注解，捕获所有异常回滚事务
    public AjaxResult importData31(@RequestBody ChartDateTto chartDateTto) throws Exception {
        // 参数校验
        String validationMessage = ChartValidator.check(chartDateTto);
        if (validationMessage != null) {
            // 返回校验失败的提示信息给前端
            return AjaxResult.error(validationMessage);
        }
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode chartValueJson;
            if (chartDateTto.getChartValue() != null) {
                chartValueJson = objectMapper.readTree(chartDateTto.getChartValue());
                if (chartDateTto.getConfigId() == 18) {
                    List<List<String>> lists = ChartDataProcessorUtils.extractDataFromList(chartDateTto.getDataList());
                    for (List<String> list : lists) {
                        System.out.println("我是configId等于18的数据" + list);
                    }
                    // 初始化 ObjectMapper
                    ObjectMapper mapper = new ObjectMapper();
                    // 解析现有的 JSON 数据
                    JsonNode jsonNode = mapper.readTree(chartDateTto.getChartValue());
                    ArrayNode datasetArray = (ArrayNode) jsonNode.path("dataset");
                    // 定义标题行
                    List<Object> biao = Arrays.asList("X", "Y", "Z");
                    // 获取图表的名称，从 lists 的第一行第二个元素获取 "充电桩1"
                    String name = "";
                    if (lists != null && lists.size() > 0) {
                        List<String> firstList = lists.get(0);
                        if (firstList.size() >= 2) {
                            name = firstList.get(1); // "充电桩1"
                            System.out.println("我是图的名称：" + name);
                        } else {
                            System.out.println("第一行数据不足 2 个元素，无法提取名称。");
                        }
                    } else {
                        System.out.println("数据列表为空，无法填充 source 数组。");
                    }
                    ArrayNode newSourceArray = mapper.createArrayNode();
                    ArrayNode headerRow = mapper.createArrayNode();
                    for (Object obj : biao) {
                        headerRow.add(obj.toString());
                    }
                    newSourceArray.add(headerRow);
                    // 遍历 lists，从第二行开始添加数据
                    if (lists != null && lists.size() > 1) {
                        for (int i = 1; i < lists.size(); i++) {
                            List<String> currentList = lists.get(i);
                            if (currentList.size() >= 2) {
                                String date = currentList.get(0);
                                String valueStr = currentList.get(1);
                                double value;

                                try {
                                    value = Double.parseDouble(valueStr);
                                } catch (NumberFormatException e) {
                                    System.out.println("无法解析数值: " + valueStr + "，将跳过此条数据。");
                                    continue;
                                }
                                // 创建数据行
                                ArrayNode dataRow = mapper.createArrayNode();
                                dataRow.add(name);    // 第一列：充电桩名称
                                dataRow.add(date);    // 第二列：日期
                                dataRow.add(value);   // 第三列：数值

                                // 添加到 newSourceArray
                                newSourceArray.add(dataRow);
                            } else {
                                System.out.println("列表元素不足 2 个，跳过此条数据: " + currentList);
                            }
                        }
                    } else {
                        System.out.println("没有足够的数据行来填充 source 数组。");
                    }
                    // 更新 dataset 中的 source 数组
                    for (JsonNode node : datasetArray) {
                        if (node.isObject()) {
                            ObjectNode datasetObject = (ObjectNode) node;
                            datasetObject.set("source", newSourceArray);
                        }
                    }
                    // 打印处理后的 JSON
                    String updatedJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode);

                    // 获取最小值和最大值
                    double[] minMaxValues = ChartDataProcessorUtils.getMinMaxValues(lists);
                    double minVs = minMaxValues[0];
                    double maxVs = minMaxValues[1];
                    String minV = String.valueOf(minVs);
                    String maxV = String.valueOf(maxVs);
                    System.out.println("最小值" + minV);
                    System.out.println("最大值" + maxV);
                    // 提取 visualMap 数组
                    ArrayNode visualMapArray = (ArrayNode) jsonNode.path("visualMap");
                    if (visualMapArray.size() > 0) {  // 确保 visualMap 数组不为空
                        // 遍历所有 visualMap 对象
                        for (int i = 0; i < visualMapArray.size(); i++) {
                            ObjectNode visualMapObject = (ObjectNode) visualMapArray.get(i); // 获取每一个对象
                            visualMapObject.put("min", Double.parseDouble(minV));  // 更新 min
                            visualMapObject.put("max", Double.parseDouble(maxV));  // 更新 max
                            System.out.println("visualMap 更新后的值: " + visualMapObject);

                            // 更新 range 数组
                            ArrayNode rangeArray = (ArrayNode) visualMapObject.path("range");
                            if (rangeArray.size() >= 2) { // 确保 range 数组至少有两个元素
                                rangeArray.set(0, new TextNode(minV)); // 更新第一个元素为 minV
                                rangeArray.set(1, new TextNode(maxV)); // 更新第二个元素为 maxV
                            } else {
                                System.out.println("range 数组元素不足，无法更新。");
                            }
                        }
                    } else {
                        System.out.println("visualMap 数组为空，无法更新 min 和 max。");
                    }
                    // 打印最终更新后的 JSON
                    updatedJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode);
                    ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
                    // 创建新的ChartSmartVo对象并设置属性
                    ChartSmartVo chartSmartVo = new ChartSmartVo();
                    chartSmartVo.setConfigId(configs.getId());
                    chartSmartVo.setConfigName(configs.getConfigName());
                    chartSmartVo.setConfigValue(updatedJson);
                    chartSmartVo.setChartTypeId(configs.getChartTypeId());
                    ArrayList<Object> newList = new ArrayList<>();
                    newList.add(chartSmartVo);
                    return AjaxResult.success(newList);
                }
                if (chartDateTto.getConfigId() == 16) {
                    List<List<String>> getdata = ChartDataProcessorUtils.extractDataFromList(chartDateTto.getDataList());
                    List<String> xList = new ArrayList<>();
                    List<String> yList = new ArrayList<>();
                    List<List<String>> dataList = new ArrayList<>();
                    ChartDataProcessorUtils.processChartData(getdata, xList, yList, dataList);
                    double[] minMaxValues = ChartDataProcessorUtils.getMinMaxValues(dataList);
                    double minV = minMaxValues[0];
                    double maxV = minMaxValues[1];
                    List<List<Object>> newDataList = ChartDataProcessorUtils.getNewDataList(xList, yList, dataList);
                    ChartDataProcessorUtils.updateChartConfiguration16(chartValueJson, newDataList, minV, maxV, xList, yList, objectMapper);
                    ChartSmartVo chartSmartVo = new ChartSmartVo();
                    chartSmartVo.setConfigValue(objectMapper.writeValueAsString(chartValueJson));

                    List<ChartSmartVo> newList = new ArrayList<>();
                    newList.add(chartSmartVo);

                    return AjaxResult.success(newList);


                }
                if (chartDateTto.getConfigId() == 19) {
                    List<List<String>> getdata = ChartDataProcessorUtils.extractDataFromList(chartDateTto.getDataList());
                    List<String> xList = new ArrayList<>();
                    List<String> yList = new ArrayList<>();
                    List<List<String>> dataList = new ArrayList<>();
                    ChartDataProcessorUtils.processChartData(getdata, xList, yList, dataList);
                    double[] minMaxValues = ChartDataProcessorUtils.getMinMaxValues(dataList);
                    double minV = minMaxValues[0];
                    double maxV = minMaxValues[1];
                    List<List<Object>> newDataList = ChartDataProcessorUtils.getNewDataList(xList, yList, dataList);
                    ChartDataProcessorUtils.updateChartConfigurations(chartValueJson, newDataList, minV, maxV, xList, yList, objectMapper);
                    ChartSmartVo chartSmartVo = new ChartSmartVo();
                    chartSmartVo.setConfigValue(objectMapper.writeValueAsString(chartValueJson));
                    List<ChartSmartVo> newList = new ArrayList<>();
                    newList.add(chartSmartVo);
                    return AjaxResult.success(newList);
                }
            } else {
                Integer id = chartDateTto.getConfigId();
                ChartConfigs chartConfigs = chartConfigsService.getById(id);
                chartValueJson = objectMapper.readTree(chartConfigs.getConfigValue());
            }

            List<List<String>> getdata = ChartDataProcessorUtils.extractDataFromList(chartDateTto.getDataList());
            List<String> xList = new ArrayList<>();
            List<String> yList = new ArrayList<>();
            List<List<String>> dataList = new ArrayList<>();
            ChartDataProcessorUtils.processChartData(getdata, xList, yList, dataList);
            double[] minMaxValues = ChartDataProcessorUtils.getMinMaxValues(dataList);
            double minV = minMaxValues[0];
            double maxV = minMaxValues[1];

            System.out.println("最小值" + minV);
            System.out.println("最大值" + maxV);

            List<List<Object>> newDataList = ChartDataProcessorUtils.getNewDataList(xList, yList, dataList);
            ChartDataProcessorUtils.updateChartConfiguration(chartValueJson, newDataList, minV, maxV, xList, yList, objectMapper);
            ChartSmartVo chartSmartVo = new ChartSmartVo();
            chartSmartVo.setConfigValue(objectMapper.writeValueAsString(chartValueJson));

            List<ChartSmartVo> newList = new ArrayList<>();
            newList.add(chartSmartVo);

            return AjaxResult.success(newList);
        } catch (Exception e) {
            log.error("接口importData3configId={}发生异常:{}", chartDateTto.getConfigId(), e.getMessage());
            throw new RuntimeException(e);
        }
    }


    private ChartSmartVo getChartV41(List<List<String>> getdata, String chartValue) {
        List<String> groupName = new ArrayList<>();
        List<List<Object>> gruopDatas = new ArrayList<>();
        JSONObject jsonObject = JSON.parseObject(chartValue);
        List<JSONObject> dataset = (List<JSONObject>) jsonObject.get("dataset");
        List<JSONObject> datasetList = new ArrayList<>();

        for (int i = 1; i < getdata.size(); i++) {
            List<Object> gruopData = new ArrayList<>();
            List<String> colData = new ArrayList<>();
            List<String> data = getdata.get(i);
            for (String s : data) {
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    colData.add(s);
                }
            }

            if (!CollectionUtils.isEmpty(colData)) {
                if (i == getdata.size() - 1) {
                    JSONObject jsonObject1 = new JSONObject();
                    for (String s : colData) {
                        gruopData.add(Double.parseDouble(s));
                    }
                    gruopDatas.add(gruopData);
                    jsonObject1.put("source", gruopDatas);
                    datasetList.add(jsonObject1);
                } else if (i == 1) {
                    groupName.add(colData.get(0));
                } else {
                    if (colData.size() == 1) {
                        groupName.add(colData.get(0));
                        JSONObject jsonObject1 = new JSONObject();
                        jsonObject1.put("source", gruopDatas);
                        datasetList.add(jsonObject1);
                        gruopDatas = new ArrayList<>();
                    } else {
                        for (String s : colData) {
                            gruopData.add(Double.parseDouble(s));
                        }
                        gruopDatas.add(gruopData);
                    }
                }
            }
        }

        List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
        ArrayList<JSONObject> seriesList = new ArrayList<>();
        JSONObject jsonObject1 = series.get(0);

        JSONObject jsonObject2 = dataset.get(dataset.size() - 1);
        for (int i = 0; i < groupName.size(); i++) {
            JSONObject jsonObject3 = new JSONObject();
            jsonObject3.putAll(jsonObject2);
            jsonObject3.put("fromDatasetIndex", i);
            datasetList.add(jsonObject3);

            JSONObject jsonObject4 = new JSONObject();
            jsonObject4.putAll(jsonObject1);
            jsonObject4.put("name", groupName.get(i));
            jsonObject4.put("datasetIndex", i + groupName.size());
            seriesList.add(jsonObject4);
        }
        jsonObject.put("dataset", datasetList);
        jsonObject.put("series", seriesList);

        Object legendObject = jsonObject.get("legend");
        if (legendObject instanceof JSONObject) {
            JSONObject legend = (JSONObject) legendObject;
            Object existingData = legend.get("data");
            if (existingData instanceof JSONArray) {
                JSONArray dataArray = (JSONArray) existingData;
                dataArray.clear();  // Clear existing data
                for (String name : groupName) {
                    JSONObject map = new JSONObject();
                    map.put("name", name);
                    dataArray.add(map);
                }
            } else {
                ArrayList<Map> maps = new ArrayList<>();
                for (String name : groupName) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("name", name);
                    maps.add(map);
                }
                legend.put("data", maps);
            }
            jsonObject.put("legend", legend);
        } else if (legendObject instanceof JSONArray) {
            JSONArray legendArray = (JSONArray) legendObject;
            for (int i = 0; i < legendArray.size(); i++) {
                JSONObject legendItem = legendArray.getJSONObject(i);
                Object existingData = legendItem.get("data");
                if (existingData instanceof JSONArray) {
                    JSONArray dataArray = (JSONArray) existingData;
                    dataArray.clear();
                    for (String name : groupName) {
                        JSONObject map = new JSONObject();
                        map.put("name", name);
                        dataArray.add(map);
                    }
                } else {
                    ArrayList<Map> maps = new ArrayList<>();
                    for (String name : groupName) {
                        Map<String, Object> map = new HashMap<>();
                        map.put("name", name);
                        maps.add(map);
                    }
                    legendItem.put("data", maps);
                }
            }
            jsonObject.put("legend", legendArray);
        } else {
            System.err.println("Unexpected type for 'legend': " + legendObject.getClass().getSimpleName());
        }

        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigValue(JSON.toJSONString(jsonObject));
        return chartSmartVo;
    }

    private ChartSmartVo getChartV52(List<List<String>> getdata, String chartValue) {
        List<String> groupName = new ArrayList<>();
        List<List<Double>> groupList = new ArrayList<>();

        List<Map> maps = new ArrayList<>();
        for (int i = 1; i < getdata.size(); i++) {
            List<String> colData = getdata.get(i);
            if (!CollectionUtils.isEmpty(colData)) {
                List<String> data = new ArrayList<>();
                for (int i1 = 0; i1 < colData.size(); i1++) {
                    String s = colData.get(i1);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        data.add(s);
                    }
                }

                if (data.size() == 1) {
                    if (i != 1) {
                        Map map = new HashMap<>();
                        map.put("source", groupList);
                        maps.add(map);
                        groupList = new ArrayList<>();
                    }
                    groupName.addAll(data);
                } else {
                    List<Double> dataList = new ArrayList<>();
                    dataList.add(Double.parseDouble(data.get(0)));
                    dataList.add(Double.parseDouble(data.get(1)));
                    groupList.add(dataList);
                    if (getdata.size() - 1 == i) {
                        Map map = new HashMap<>();
                        map.put("source", groupList);
                        maps.add(map);
                    }
                }
            }
        }

        //  ChartConfigs configs = chartConfigsService.getById(23);
        JSONObject jsonObject = JSON.parseObject(chartValue);
        jsonObject.put("dataset", maps);

        List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
        JSONObject jsonObject1 = series.get(0);

        ArrayList<JSONObject> jsonObjects = new ArrayList<>();
        for (int i = 0; i < groupName.size(); i++) {
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.putAll(jsonObject1);
            jsonObject2.put("datasetIndex", i);
            jsonObject2.put("name", groupName.get(i));
            jsonObjects.add(jsonObject2);
        }
        jsonObject.put("series", jsonObjects);
        ChartSmartVo chartSmartVo = new ChartSmartVo();

        chartSmartVo.setConfigValue(JSON.toJSONString(jsonObject));
        return chartSmartVo;
    }

    @ApiOperation("散点图 多元线性回归图 极坐标图")
    @PostMapping("/importData5")
    @Transactional(rollbackFor = Exception.class) // 添加事务管理注解，捕获所有异常回滚事务
    public AjaxResult importData5(@RequestBody ChartDateTto chartDateTto) throws Exception {
        // 参数校验
        String validationMessage = ChartValidator.check(chartDateTto);
        if (validationMessage != null) {
            // 返回校验失败的提示信息给前端
            return AjaxResult.error(validationMessage);
        }
        try {
            if (chartDateTto.getChartValue() != null) {
                if (chartDateTto.getConfigId() == 23) {//多元线性回归图
                    System.out.println(chartDateTto.getDataList());
                    Sheet sheet = ExcelDataUtils.convertDataListToSheetsa(chartDateTto.getDataList());
                    String chartValue = chartDateTto.getChartValue();
                    //多元线性回归图
                    //                    list.addAll(colData);
                    ChartSmartVo chartV52 = getChartV52(ExcelDataUtils.getData(sheet), chartValue);

                    ArrayList<ChartSmartVo> newList = new ArrayList<>();
                    newList.add(chartV52);
                    System.out.println(chartV52.getConfigValue().toString());
                    return AjaxResult.success(newList);

                    //多元线性回归图
                } else if (chartDateTto.getConfigId() == 15) {//极坐标图

                    return AjaxResult.success("还在制作中");


                } else if (chartDateTto.getConfigId() == 23) {//线性回归图
                    String chartValue = chartDateTto.getChartValue();
                    Sheet sheet = ExcelDataUtils.convertDataListToSheetsa(chartDateTto.getDataList());
                    ChartSmartVo chartV53 = getChartV53(ExcelDataUtils.getData(sheet), chartValue);
                    ArrayList<ChartSmartVo> newList = new ArrayList<>();
                    newList.add(chartV53);
                    return AjaxResult.success(newList);
                }
                String chartValue = chartDateTto.getChartValue();
                List<List<String>> lists = ChartDataUtils.extractDataFromList(chartDateTto.getDataList());
                int size = lists.get(0).size(); // 假设所有列长度一致
                if (size % 2 == 0) {
                    int group = size / 2;
                    ObjectMapper objectMapper = new ObjectMapper();
                    JsonNode chartValueJson = objectMapper.readTree(chartValue);
                    // 获取 dataset 节点并清空 source
                    ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
                    dataset.removeAll();
                    // 根据 group 的数量重新建立 source
                    for (int i = 0; i < group; i++) {
                        ArrayNode newSource = objectMapper.createArrayNode();
                        for (List<String> list : lists) {
                            if (list.size() > i * 2) {
                                ArrayNode pair = objectMapper.createArrayNode();
                                pair.add(list.get(i * 2)); // 第一个数据
                                if (list.size() > i * 2 + 1) {
                                    pair.add(list.get(i * 2 + 1)); // 第二个数据
                                }
                                newSource.add(pair);
                            }
                        }
                        ObjectNode newDatasetObject = objectMapper.createObjectNode();
                        newDatasetObject.set("source", newSource);
                        dataset.add(newDatasetObject);
                    }
                    // 获取 series 节点并清空
                    ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");
                    seriesArray.removeAll();

                    // 重新构建 series，每两个数据为一组
                    for (int i = 0; i < group; i++) {
                        ObjectNode seriesObject = objectMapper.createObjectNode();
                        seriesObject.put("name", lists.get(0).get(i * 2)); // 使用第一个数据作为 name
                        seriesObject.put("type", "scatter"); // 假设图表类型为 scatter，可以根据需要调整
                        seriesObject.put("datasetIndex", i);
                        seriesArray.add(seriesObject);
                    }

                    // 获取 legend 节点并更新 data
                    JsonNode legend = chartValueJson.path("legend");
                    ChartDataUtils.updateLegendData(legend, lists);

                    // 处理颜色字段
                    ObjectNode jsonNode = (ObjectNode) objectMapper.readTree(chartValue);
                    // 获取 color 字段
                    JsonNode colorNode = jsonNode.path("color");

                    List<String> baseColors = new ArrayList<>();
                    // 遍历 color 数组并将每个颜色值添加到 baseColors 集合中
                    if (colorNode.isArray()) {
                        for (JsonNode color : colorNode) {
                            String colorValue = color.asText();
                            baseColors.add(colorValue);
                        }
                    }

                    int beforeSize = baseColors.size();
                    int afterSize = lists.get(0).size();
                    if (beforeSize < afterSize) {
                        chartValueJson = ColorUtils.processChartColorsHert(JSON.toJSONString(chartValueJson), afterSize);
                    }


                    String updatedChartValue = chartValueJson.toString();
                    ArrayList<ChartSmartVo> newList = new ArrayList<>();
                    ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());

                    ChartSmartVo chartSmartVo = new ChartSmartVo();
                    chartSmartVo.setConfigId(configs.getId());
                    chartSmartVo.setConfigName(configs.getConfigName());
                    chartSmartVo.setConfigValue(updatedChartValue);
                    chartSmartVo.setChartTypeId(configs.getChartTypeId());
                    newList.add(chartSmartVo);
                    System.out.println(updatedChartValue);
                    return AjaxResult.success(newList);
                } else {
                    return AjaxResult.error("请输入偶数的数据列");
                }

            } else {
                //根据id查询chart_configs
                ChartConfigs chartConfigs = chartConfigsService.getById(chartDateTto.getConfigId());
                String configValue = chartConfigs.getConfigValue();
                List<List<String>> lists = ChartDataUtils.extractDataFromList(chartDateTto.getDataList());
                int size = lists.get(0).size(); // 假设所有列长度一致
                if (size % 2 == 0) {
                    int group = size / 2;
                    ObjectMapper objectMapper = new ObjectMapper();
                    JsonNode chartValueJson = objectMapper.readTree(configValue);
                    // 获取 dataset 节点并清空 source
                    ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
                    dataset.removeAll();
                    // 根据 group 的数量重新建立 source
                    for (int i = 0; i < group; i++) {
                        ArrayNode newSource = objectMapper.createArrayNode();
                        for (List<String> list : lists) {
                            if (list.size() > i * 2) {
                                ArrayNode pair = objectMapper.createArrayNode();
                                pair.add(list.get(i * 2)); // 第一个数据
                                if (list.size() > i * 2 + 1) {
                                    pair.add(list.get(i * 2 + 1)); // 第二个数据
                                }
                                newSource.add(pair);
                            }
                        }
                        ObjectNode newDatasetObject = objectMapper.createObjectNode();
                        newDatasetObject.set("source", newSource);
                        dataset.add(newDatasetObject);
                    }
                    // 获取 series 节点并清空
                    ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");
                    seriesArray.removeAll();

                    // 重新构建 series，每两个数据为一组
                    for (int i = 0; i < group; i++) {
                        ObjectNode seriesObject = objectMapper.createObjectNode();
                        seriesObject.put("name", lists.get(0).get(i * 2)); // 使用第一个数据作为 name
                        seriesObject.put("type", "scatter"); // 假设图表类型为 scatter，可以根据需要调整
                        seriesObject.put("datasetIndex", i);
                        seriesArray.add(seriesObject);
                    }

                    // 获取 legend 节点并更新 data
                    JsonNode legend = chartValueJson.path("legend");
                    ChartDataUtils.updateLegendData(legend, lists);
                    String updatedChartValue = chartValueJson.toString();
                    ArrayList<ChartSmartVo> newList = new ArrayList<>();
                    ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
                    ChartSmartVo chartSmartVo = new ChartSmartVo();
                    chartSmartVo.setConfigId(configs.getId());
                    chartSmartVo.setConfigName(configs.getConfigName());
                    chartSmartVo.setConfigValue(updatedChartValue);
                    chartSmartVo.setChartTypeId(configs.getChartTypeId());
                    newList.add(chartSmartVo);
                    System.out.println(updatedChartValue);
                    return AjaxResult.success(newList);
                } else {
                    return AjaxResult.error("请输入偶数的数据列");
                }
            }
        } catch (Exception e) {
            log.error("importData5接口的configId:{}发生异常:{}", chartDateTto.getConfigId(), e.getMessage());
            throw new RuntimeException(e);
        }


    }


    public static List<List<Object>> convertDataList(List<Map<String, Object>> dataList) {
        List<List<Object>> resultList = new ArrayList<>();
        for (Map<String, Object> map : dataList) {
            List<Object> row = new ArrayList<>();
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                row.add(entry.getValue());
            }
            resultList.add(row);
        }
        return resultList;
    }


    private ChartSmartVo getChartV63(List<List<String>> getdata, String chartValue) throws Exception {
        List<JSONObject> list = new ArrayList<>();
        List<String> name = new ArrayList<>();

        for (List<String> strings : getdata) {
            JSONObject jsonObject = new JSONObject();
            for (int i1 = 0; i1 < strings.size(); i1++) {
                String s = strings.get(i1);
                if (org.apache.commons.lang3.ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                    if (i1 == 0) {
                        jsonObject.put("name", s);
                        name.add(s);
                    } else if (i1 == 1) {
                        jsonObject.put("value", Double.parseDouble(s));
                    }
                }
            }
            list.add(jsonObject);
        }

        JSONObject jsonObject = JSON.parseObject(chartValue);

        List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
        for (JSONObject jsonObject1 : series) {
            jsonObject1.put("data", list);
        }

        Object legendObject = jsonObject.get("legend");
        if (legendObject instanceof JSONObject) {
            JSONObject legend = (JSONObject) legendObject;
            JSONArray dataArray = legend.getJSONArray("data");
            if (dataArray == null) {
                dataArray = new JSONArray();
            }
            dataArray.clear();
            for (String nameItem : name) {
                JSONObject map = new JSONObject();
                map.put("name", nameItem);
                dataArray.add(map);
            }
            legend.put("data", dataArray);
        } else if (legendObject instanceof JSONArray) {
            JSONArray legendArray = (JSONArray) legendObject;
            for (int i = 0; i < legendArray.size(); i++) {
                JSONObject legendItem = legendArray.getJSONObject(i);
                JSONArray dataArray = legendItem.getJSONArray("data");
                if (dataArray == null) {
                    dataArray = new JSONArray();
                }
                dataArray.clear();
                for (String nameItem : name) {
                    JSONObject map = new JSONObject();
                    map.put("name", nameItem);
                    dataArray.add(map);
                }
                legendItem.put("data", dataArray);
            }
        } else {
            JSONObject legend = new JSONObject();
            JSONArray dataArray = new JSONArray();
            for (String nameItem : name) {
                JSONObject map = new JSONObject();
                map.put("name", nameItem);
                dataArray.add(map);
            }
            legend.put("data", dataArray);
            jsonObject.put("legend", legend);
        }

        jsonObject.put("series", series);

        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigValue(JSON.toJSONString(jsonObject));
        return chartSmartVo;
    }


    @ApiOperation("南丁格尔图、扇形图，圆角环形图  漏斗图")
    @PostMapping("importData6")
    @Transactional(rollbackFor = Exception.class) // 添加事务管理注解，捕获所有异常回滚事务
    public AjaxResult importData6(@RequestBody ChartDateTto chartDateTto) throws Exception {
        // 参数校验
        String validationMessage = ChartValidator.check(chartDateTto);
        if (validationMessage != null) {
            // 返回校验失败的提示信息给前端
            return AjaxResult.error(validationMessage);
        }
        try {
            if (chartDateTto.getChartValue() != null) {
                if (chartDateTto.getConfigId() == 41) {
                    Sheet sheet = ExcelDataUtils.convertDataListToSheetsa(chartDateTto.getDataList());
                    String chartValue = chartDateTto.getChartValue();
                    // 漏斗图
                    ChartSmartVo chartV63 = getChartV63(ExcelDataUtils.getData(sheet), chartValue);
                    String configValue = chartV63.getConfigValue();
                    ObjectMapper objectMapper = new ObjectMapper();

                    // 处理颜色字段
                    ObjectNode jsonNode = (ObjectNode) objectMapper.readTree(configValue);
                    // 获取 color 字段
                    JsonNode colorNode = jsonNode.path("color");

                    List<String> baseColors = new ArrayList<>();
                    // 遍历 color 数组并将每个颜色值添加到 baseColors 集合中
                    if (colorNode.isArray()) {
                        for (JsonNode color : colorNode) {
                            String colorValue = color.asText();
                            baseColors.add(colorValue);
                        }
                    }

                    List<Map<String, Object>> dataList = chartDateTto.getDataList();
                    int beforeSize = baseColors.size();
                    int afterSize = dataList.size();

                    for (Map<String, Object> stringObjectMap : dataList) {
                        System.out.println("我是dataList的数据" + stringObjectMap);
                    }


                    // 如果 beforeSize 小于 afterSize，调用 processChartColorsHert 更新颜色
                    if (beforeSize < afterSize) {
                        // 这里处理 configValue 并返回更新后的 ObjectNode
                        ObjectNode updatedJsonNode = ColorUtils.processChartColorsHert(configValue, afterSize);

                        // 将更新后的 ObjectNode 转换回字符串并设置回 ChartSmartVo 对象
                        String updatedConfigValue = objectMapper.writeValueAsString(updatedJsonNode);
                        chartV63.setConfigValue(updatedConfigValue);
                    }

                    ArrayList<ChartSmartVo> newList = new ArrayList<>();
                    newList.add(chartV63);
                    System.out.println(chartV63.getConfigValue().toString());
                    return AjaxResult.success(newList);

                } else {
                    List<Map<String, Object>> dataList = chartDateTto.getDataList();
                    // 创建两个内容都设置为 0 的对象
                    Map<String, Object> firstObject = new HashMap<>();
                    firstObject.put("key1", 0); // 设置键值对
                    Map<String, Object> secondObject = new HashMap<>();
                    secondObject.put("key2", 0); // 设置键值对

                    // 将对象插入到 dataList 的最前面
                    dataList.add(0, secondObject);
                    dataList.add(0, firstObject);
                    String chartValue = chartDateTto.getChartValue();
                    // List<Map<String, Object>> originalMapList = chartDateTto.getDataList();

                    // 移除第一个对象
                    if (!dataList.isEmpty()) {
                        dataList.remove(0);
                    }

                    // 将 List<Map<String, Object>> 转换为 List<List<Object>>
                    List<List<Object>> lists = convertDataList(dataList);

                    // 从第二个list开始，每个list的元素除了第一个，其他转换为整数类型
                    for (int i = 1; i < lists.size(); i++) {
                        List<Object> row = lists.get(i);
                        for (int j = 1; j < row.size(); j++) {
                            if (row.get(j) instanceof String) {
                                try {
                                    row.set(j, Double.parseDouble((String) row.get(j)));
                                } catch (NumberFormatException e) {
                                    // 如果转换失败，保留原字符串
                                }
                            }
                        }
                    }

                    // 移除第一个对象
                    List<List<Object>> modifiedLists = new ArrayList<>(lists);
                    if (!modifiedLists.isEmpty()) {
                        modifiedLists.remove(0);
                    }

                    ObjectMapper objectMapper = new ObjectMapper();
                    JsonNode chartValueJson = objectMapper.readTree(chartValue);
                    // 获取 dataset 节点
                    ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");

                    // 找到 source 节点并替换它，保留第一个对象
                    for (JsonNode node : dataset) {
                        if (node.has("source")) {
                            ArrayNode originalSource = (ArrayNode) node.get("source");
                            ArrayNode newSource = objectMapper.createArrayNode();

                            // 保留第一个对象
                            if (originalSource.size() > 0) {
                                newSource.add(originalSource.get(0));
                            }

                            // 将修改后的数据逐项添加到 newSource 中
                            ArrayNode modifiedSource = objectMapper.valueToTree(modifiedLists);
                            for (JsonNode jsonNode : modifiedSource) {
                                newSource.add(jsonNode);
                            }

                            ((ObjectNode) node).set("source", newSource);
                            break;
                        }
                    }

                    // 处理颜色字段
                    ObjectNode jsonNode = (ObjectNode) objectMapper.readTree(chartValue);
                    // 获取 color 字段
                    JsonNode colorNode = jsonNode.path("color");

                    List<String> baseColors = new ArrayList<>();
                    // 遍历 color 数组并将每个颜色值添加到 baseColors 集合中
                    if (colorNode.isArray()) {
                        for (JsonNode color : colorNode) {
                            String colorValue = color.asText();
                            baseColors.add(colorValue);
                        }
                    }

                    int beforeSize = baseColors.size();
                    int afterSize = dataList.size() - 1;
                    if (beforeSize < afterSize) {
                        chartValueJson = ColorUtils.processChartColorsHert(JSON.toJSONString(chartValueJson), afterSize);
                    }

                    String updatedChartValue = chartValueJson.toString();
                    ArrayList<ChartSmartVo> newList = new ArrayList<>();
                    // 获取 ChartConfigs 对象
                    ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
                    if (ObjectUtils.isEmpty(configs)) {
                        log.error("未找到图表配置。配置ID={}", chartDateTto.getConfigId());
                        return AjaxResult.error("未找到该图表的配置，请检查配置ID是否正确。");
                    }
                    // 创建新的 ChartSmartVo 对象并设置属性
                    ChartSmartVo chartSmartVo = new ChartSmartVo();
                    chartSmartVo.setConfigId(configs.getId()); // 假设 ChartConfigs 有 getId 方法
                    chartSmartVo.setConfigName(configs.getConfigName()); // 假设 ChartConfigs 有 getName 方法
                    chartSmartVo.setConfigValue(updatedChartValue);
                    chartSmartVo.setChartTypeId(configs.getChartTypeId());

                    newList.add(chartSmartVo);
                    System.out.println(updatedChartValue);
                    return AjaxResult.success(newList);
                }
            } else {
                List<Map<String, Object>> dataList = chartDateTto.getDataList();
                // 创建两个内容都设置为 0 的对象
                Map<String, Object> firstObject = new HashMap<>();
                firstObject.put("key1", 0); // 设置键值对
                Map<String, Object> secondObject = new HashMap<>();
                secondObject.put("key2", 0); // 设置键值对

                // 将对象插入到 dataList 的最前面
                dataList.add(0, secondObject);
                dataList.add(0, firstObject);
                //chart_configs_interface的config_id 等于 chart_configs的id字段
                Integer id = chartDateTto.getConfigId();
                //根据id查询chart_configs
                ChartConfigs chartConfigs = chartConfigsService.getById(id);
                if (ObjectUtils.isEmpty(chartConfigs)) {
                    log.error("未找到图表配置。配置ID={}", chartDateTto.getConfigId());
                    return AjaxResult.error("未找到该图表的配置，请检查配置ID是否正确。");
                }
                //模版
                String chartValue = chartConfigs.getConfigValue();
                // 移除第一个对象
                if (!dataList.isEmpty()) {
                    dataList.remove(0);
                }
                // 将 List<Map<String, Object>> 转换为 List<List<Object>>
                List<List<Object>> lists = convertDataList(dataList);

                // 从第二个list开始，每个list的元素除了第一个，其他转换为整数类型
                for (int i = 1; i < lists.size(); i++) {
                    List<Object> row = lists.get(i);
                    for (int j = 1; j < row.size(); j++) {
                        if (row.get(j) instanceof String) {
                            try {
                                row.set(j, Double.parseDouble((String) row.get(j)));
                            } catch (NumberFormatException e) {
                                // 如果转换失败，保留原字符串
                            }
                        }
                    }
                }

                // 移除第一个对象
                List<List<Object>> modifiedLists = new ArrayList<>(lists);
                if (!modifiedLists.isEmpty()) {
                    modifiedLists.remove(0);
                }

                ObjectMapper objectMapper = new ObjectMapper();
                JsonNode chartValueJson = objectMapper.readTree(chartValue);
                // 获取 dataset 节点
                ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");

                // 找到 source 节点并替换它，保留第一个对象
                for (JsonNode node : dataset) {
                    if (node.has("source")) {
                        ArrayNode originalSource = (ArrayNode) node.get("source");
                        ArrayNode newSource = objectMapper.createArrayNode();

                        // 保留第一个对象
                        if (originalSource.size() > 0) {
                            newSource.add(originalSource.get(0));
                        }

                        // 将修改后的数据逐项添加到 newSource 中
                        ArrayNode modifiedSource = objectMapper.valueToTree(modifiedLists);
                        for (JsonNode jsonNode : modifiedSource) {
                            newSource.add(jsonNode);
                        }

                        ((ObjectNode) node).set("source", newSource);
                        break;
                    }
                }

                String updatedChartValue = chartValueJson.toString();
                ArrayList<ChartSmartVo> newList = new ArrayList<>();
                // 获取 ChartConfigs 对象
                ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
                if (ObjectUtils.isEmpty(configs)) {
                    log.error("未找到图表配置。配置ID={}", chartDateTto.getConfigId());
                    return AjaxResult.error("未找到该图表的配置，请检查配置ID是否正确。");
                }
                // 创建新的 ChartSmartVo 对象并设置属性
                ChartSmartVo chartSmartVo = new ChartSmartVo();
                chartSmartVo.setConfigId(configs.getId()); // 假设 ChartConfigs 有 getId 方法
                chartSmartVo.setConfigName(configs.getConfigName()); // 假设 ChartConfigs 有 getName 方法
                chartSmartVo.setConfigValue(updatedChartValue);
                chartSmartVo.setChartTypeId(configs.getChartTypeId());
                newList.add(chartSmartVo);
                System.out.println(updatedChartValue);
                return AjaxResult.success(newList);
            }
        } catch (Exception e) {
            log.error("importData6的configId:{}发生异常:{}", chartDateTto.getConfigId(), e.getMessage());
            throw new RuntimeException(e);
        }

    }


    private List<List<String>> extractDataFromList(List<Map<String, Object>> dataList) {
        List<List<String>> extractedData = new ArrayList<>();

        for (Map<String, Object> row : dataList) {

            List<String> rowData = new ArrayList<>();
            for (Map.Entry<String, Object> entry : row.entrySet()) {
                rowData.add(entry.getValue().toString());
            }

            extractedData.add(rowData);
        }
        return extractedData;
    }


    private static ChartSmartVo getChartV29(List<List<String>> getdata, String chartValue) {
        // 提取 xdata
        List<String> xdata = new ArrayList<>();
        List<String> xList = getdata.get(0);
        if (!CollectionUtils.isEmpty(xList)) {
            for (String s : xList) {
                if (!ObjectUtils.isEmpty(s)) {
                    xdata.add(s);
                }
            }
        }

        // 处理 datas
        List<JSONObject> datas = new ArrayList<>();
        for (int i = 1; i < getdata.size(); i++) {
            List<List<Object>> groupList = new ArrayList<>();
            String groupName = "";
            List<String> colData = getdata.get(i);
            for (int j = 0; j < colData.size(); j++) {
                String s = colData.get(j);
                if (!ObjectUtils.isEmpty(s)) {
                    if (j == 0) {
                        groupName = s;
                    } else {
                        List<Object> cellData = new ArrayList<>();
                        cellData.add(Double.parseDouble(s));
                        groupList.add(cellData);
                    }
                }
            }

            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("value", groupList);
            jsonObject2.put("name", groupName);
            jsonObject2.put("areaStyle", new JSONObject());
            datas.add(jsonObject2);
        }

        // 处理 radarList
        List<ChartLegendData> radarList = new ArrayList<>();
        for (int i = 1; i < xdata.size(); i++) {
            ChartLegendData chartLegendData = new ChartLegendData();
            chartLegendData.setName(xdata.get(i));
            radarList.add(chartLegendData);
        }

        // 解析 chartValue
        JSONObject jsonObject = JSON.parseObject(chartValue);

        // 处理 radar 和 series
        JSONArray seriesArray = jsonObject.getJSONArray("series");
        JSONArray radarArray = jsonObject.getJSONArray("radar");

        if (radarArray != null && seriesArray != null) {
            for (int i = 0; i < radarArray.size(); i++) {
                JSONObject radarObject = radarArray.getJSONObject(i);
                radarObject.put("indicator", radarList);

                if (i < seriesArray.size()) {
                    JSONObject seriesObject = seriesArray.getJSONObject(i);
                    seriesObject.put("data", datas);
                }
            }
            jsonObject.put("radar", radarArray);
            jsonObject.put("series", seriesArray);
        }

        // 处理 legend
        JSONArray legendArray = jsonObject.getJSONArray("legend");
        if (legendArray != null) {
            for (int i = 0; i < legendArray.size(); i++) {
                JSONObject legendObject = legendArray.getJSONObject(i);
                ArrayList<Map<String, String>> legendData = new ArrayList<>();
                for (JSONObject dataObject : datas) {
                    String name = dataObject.getString("name");
                    HashMap<String, String> map = new HashMap<>();
                    map.put("name", name);
                    legendData.add(map);
                }
                legendObject.put("data", legendData);
            }
        }

        // 构建返回值
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigValue(jsonObject.toJSONString());
        return chartSmartVo;
    }


    private ChartSmartVo getChartV53(List<List<String>> getdata, String chartValue) {
        List<String> groupName = new ArrayList<>();

        List<List<List<String>>> newList = new ArrayList<>();

        List<List<String>> list = new ArrayList<>();


        for (int i = 1; i < getdata.size(); i++) {
            List<String> sdata = getdata.get(i);
            if (!CollectionUtils.isEmpty(sdata)) {
                List<String> data = new ArrayList<>();
                for (int i1 = 0; i1 < sdata.size(); i1++) {
                    String s = sdata.get(i1);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        data.add(s);
                    }
                }

                if (!CollectionUtils.isEmpty(data)) {
                    if (i == getdata.size() - 1) {
                        list.add(data);
                        newList.add(list);
                    } else if (i == 1) {
                        groupName.add(data.get(0));
                    } else {
                        if (getdata.get(i).size() == 1) {
                            groupName.add(data.get(0));
                            newList.add(list);
                            list = new ArrayList<>();
                        } else {
                            list.add(data);
                        }
                    }
                }
            }
        }

        List<Map> maps = new ArrayList<>();

        // ChartConfigs configs = chartConfigsService.getById(15);
        JSONObject jsonObject = JSON.parseObject(chartValue);
        List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
        JSONObject json = series.get(0);

        ArrayList<JSONObject> jsonObjects = new ArrayList<>();


        for (int i = 0; i < newList.size(); i++) {
            List<List<String>> lists = newList.get(i);
            HashMap<String, Object> map = new HashMap<>();
            map.put("source", lists);
            maps.add(map);

            double[] x = new double[lists.size()];
            double[] y = new double[lists.size()];

            for (int i1 = 0; i1 < lists.size(); i1++) {
                List<String> sources = lists.get(i1);
                for (int i2 = 0; i2 < sources.size(); i2++) {
                    x[i1] = Double.parseDouble(sources.get(0));
                    y[i1] = Double.parseDouble(sources.get(1));
                }
            }
            Map service = ExcelDataUtils.getService1(x, y);
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.putAll(json);
            jsonObject1.put("datasetIndex", i);
            jsonObject1.put("name", groupName.get(i));
            JSONObject markLine1 = (JSONObject) jsonObject1.get("markLine");
            JSONObject markLine = new JSONObject();
            markLine.putAll(markLine1);
            JSONObject label1 = (JSONObject) markLine.get("label");
            JSONObject label = new JSONObject();
            label.putAll(label1);
            label.put("formatter", service.get("formatter"));
            markLine.put("label", label);

            JSONObject tooltip1 = (JSONObject) markLine.get("tooltip");
            JSONObject tooltip = new JSONObject();
            tooltip.putAll(tooltip1);
            tooltip.put("formatter", service.get("formatter"));
            markLine.put("tooltip", tooltip);

            List<JSONObject> data = (List<JSONObject>) markLine.get("data");
            List<JSONObject> data1 = (List<JSONObject>) data.get(0);
            List<JSONObject> jsonObjects1 = new ArrayList<>();
            List<List<JSONObject>> jsonObjects2 = new ArrayList<>();
            for (int i1 = 0; i1 < data1.size(); i1++) {
                JSONObject jsonObject2 = data1.get(i1);
                JSONObject jsonObject3 = new JSONObject();
                jsonObject3.putAll(jsonObject2);
                if (i1 == 0) {
                    jsonObject3.put("coord", service.get("min"));
                } else {
                    jsonObject3.put("coord", service.get("max"));
                }
                jsonObjects1.add(jsonObject3);
            }
            jsonObjects2.add(jsonObjects1);
            markLine.put("data", jsonObjects2);
            jsonObject1.put("markLine", markLine);
            jsonObjects.add(jsonObject1);
        }

        JSONObject legend = (JSONObject) jsonObject.get("legend");
        ArrayList<Map> maps1 = new ArrayList<>();
        for (int i = 0; i < groupName.size(); i++) {
            Map map = new HashMap<>();
            map.put("name", groupName.get(i));
            maps1.add(map);
        }

        legend.put("data", maps1);
        jsonObject.put("legend", legend);


        jsonObject.put("series", jsonObjects);
        jsonObject.put("dataset", maps);

        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigValue(JSON.toJSONString(jsonObject));
        return chartSmartVo;
    }


    /**
     * 将原始列表分割成大小为 groupSize 的多个子列表。
     *
     * @param originalList 原始列表
     * @param groupSize    每个子列表的大小
     * @return 包含多个子列表的列表
     */
    public static List<List<Object>> splitList(List<Object> originalList, int groupSize) {
        List<List<Object>> groupedLists = new ArrayList<>();
        int size = originalList.size();
        for (int i = 0; i < size; i += groupSize) {
            int end = Math.min(size, i + groupSize);
            List<Object> sublist = originalList.subList(i, end);
            groupedLists.add(new ArrayList<>(sublist));
        }
        return groupedLists;
    }


    @ApiOperation("导入盒须图数据4")
    @PostMapping("/importData4")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult importData4(@RequestBody ChartDateTto chartDateTto) throws Exception {
        // 参数校验
        String validationMessage = ChartValidator.check(chartDateTto);
        if (validationMessage != null) {
            // 返回校验失败的提示信息给前端
            return AjaxResult.error(validationMessage);
        }
        try {
            // 提取数据
            List<List<String>> getdata = extractDataFromListNullString(chartDateTto.getDataList());
            int sizeLine = getdata.size();
            ArrayList<String> legendNameList = new ArrayList<>();//X轴名称 现在修改为图例
            // 获取第一行数据（数据名行）
            List<String> strings = getdata.get(0);
            int previousNonZeroIndex = -1;
            ArrayList<Object> arrayList = new ArrayList<>();
            for (int i = 1; i < strings.size(); i++) {

                // 检查是否是非零值（注意此处 "0" 是字符串形式）

                String value = strings.get(i);
                // 检查是否是非零值（注意此处 "0" 是字符串形式）
                if (!value.equals("0")) {
                    legendNameList.add(value);
                }
                if (!value.equals("0")) {
                    if (previousNonZeroIndex != -1) {
                        int columnsBetween = i - previousNonZeroIndex;
                        arrayList.add(columnsBetween);
                    }
                    previousNonZeroIndex = i;
                }
            }
            int lenSize = legendNameList.size();
            ArrayList<String> xAxisNameList = new ArrayList<>(); //图例名称 现在修改x轴名称
            // 获取第一行数据（数据名行）
            List<String> dataRow = getdata.get(1);
            int lastNonZeroIndex = -1;
            ArrayList<Object> columnGaps = new ArrayList<>();
            for (int i = 1; i < dataRow.size(); i++) {
                // 获取当前的值
                String currentValue = dataRow.get(i);
                // 检查是否是非零值（注意此处 "0" 是字符串形式）
                if (!currentValue.equals("0")) {
                    xAxisNameList.add(currentValue);
                }
                // 处理非零值之间的列间隔
                if (!currentValue.equals("0")) {
                    if (lastNonZeroIndex != -1) {
                        int columnsBetween = i - lastNonZeroIndex;
                        columnGaps.add(columnsBetween);
                    }
                    lastNonZeroIndex = i;
                }
            }

            // 移除第一行和第二行（如果需要）
            getdata.remove(0);
            getdata.remove(0);
            Object arr0 = arrayList.get(0);
            Integer integerValue = (Integer) arr0;
            int line = integerValue;
            ArrayList<String> selectedXAxisNames = new ArrayList<>();//图例名称
            for (int i = 0; i < Math.min(line, xAxisNameList.size()); i++) {
                selectedXAxisNames.add(xAxisNameList.get(i));
            }

            int number = sizeLine / line; //列的数据
            // 创建一个ArrayList的List来存储每一列的数据
            List<List<Double>> columnData = new ArrayList<>();
            // 初始化columnData，假设getdata不为空且至少有一行数据
            if (!getdata.isEmpty()) {
                int numColumns = getdata.get(0).size() - 1; // 减去1，因为我们要移除每行的第一列
                for (int i = 0; i < numColumns; i++) {
                    columnData.add(new ArrayList<>());
                }
            }

            // 获取seriesName列表
            ArrayList<String> seriesName = new ArrayList<>();

            // 遍历excel的每一行数据
            for (int rowIndex = 0; rowIndex < getdata.size(); rowIndex++) {
                List<String> row = getdata.get(rowIndex);
                String firstColumnValue = row.remove(0); // 移除第一列并获取其值
                seriesName.add(firstColumnValue);

                for (int colIndex = 0; colIndex < row.size(); colIndex++) {
                    String cellValue = row.get(colIndex);
                    try {
                        double numericValue = Double.parseDouble(cellValue);
                        columnData.get(colIndex).add(numericValue);
                    } catch (NumberFormatException e) {
                        System.err.println("Invalid number format: " + cellValue);
                    }
                }
            }

            ArrayList<Object> arrayLists = new ArrayList<>();
            for (int colIndex = 0; colIndex < columnData.size(); colIndex++) {
                arrayLists.add(columnData.get(colIndex));
            }

            // 分割 arrayLists
            List<List<Object>> groupedLists = splitList(arrayLists, line);

            // 打印分组后的数据
            for (int i = 0; i < groupedLists.size(); i++) {
                System.out.println("Group " + (i + 1) + ": " + groupedLists.get(i));
            }

            // 以下操作json
            String chartValue = chartDateTto.getChartValue();
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode rootNode = objectMapper.readTree(chartValue);
            ArrayNode dataset = (ArrayNode) rootNode.path("dataset");

            // 查找并保留第一个含有 source 的对象，删除其他含有 source 的对象
            JsonNode firstSourceNode = null;
            int firstSourceNodeIndex = -1;
            for (int i = 0; i < dataset.size(); i++) {
                JsonNode node = dataset.get(i);
                if (node.has("source")) {
                    if (firstSourceNode == null) {
                        // 记录第一个含有 source 的对象，并保存其索引
                        firstSourceNode = node;
                        firstSourceNodeIndex = i;
                    } else {
                        // 删除其他含有 source 的对象
                        dataset.remove(i);
                        i--; // 调整索引，因为我们删除了一个元素
                    }
                }
            }

            // 根据 groupedLists.size() 的值复制 firstSourceNode，并添加到 dataset 中
            int requiredDatasets = groupedLists.size();
            int insertIndex = firstSourceNodeIndex + 1; // 插入位置在第一个 source 对象之后
            for (int i = 1; i < requiredDatasets; i++) { // 从1开始，因为第一个已经存在
                dataset.insert(insertIndex, firstSourceNode.deepCopy());
                insertIndex++; // 每次插入后，更新插入位置
            }

            // 更新 dataset 中每个 source 对象的 source 数组值
            int groupIndex = 0;
            for (JsonNode node : dataset) {
                if (node.has("source")) {
                    ArrayNode sourceArray = (ArrayNode) node.get("source");

                    // 确保 sourceArray 与 groupedLists 的大小一致
                    sourceArray.removeAll();
                    List<Object> groupData = groupedLists.get(groupIndex);
                    for (Object item : groupData) {
                        // 将每个项转换为 JsonNode
                        ArrayNode arrayNode = objectMapper.createArrayNode();
                        ((List<Double>) item).forEach(arrayNode::add);
                        sourceArray.add(arrayNode);
                    }
                    groupIndex++;
                }
            }

            //处理 legend 中的data 要求取出
            ArrayNode legend = (ArrayNode) rootNode.path("legend");
            // 获取 legend 的 data 数组
            ArrayNode legendDataArray = (ArrayNode) legend.get(0).get("data");
            // 保留 data 数组中的第一个对象
            JsonNode firstLegendData = legendDataArray.get(0);
            // 删除剩余的对象
            legendDataArray.removeAll();
            legendDataArray.add(firstLegendData);

            // 根据 selectedXAxisNames.size() 进行复制第一个对象，并替换 name 的值
            for (int i = 0; i < legendNameList.size(); i++) {
                // 复制第一个对象
                JsonNode newLegendData = firstLegendData.deepCopy();

                // 替换 name 的值
                ((ObjectNode) newLegendData).put("name", legendNameList.get(i));

                // 将新对象添加到 data 数组中
                legendDataArray.add(newLegendData);
            }
            // 复制完成后删除第一个对象
            legendDataArray.remove(0);


            // 处理  series
            ArrayNode series = (ArrayNode) rootNode.path("series");
            // 获取 series 数组中的第一个对象
            JsonNode firstSeries = series.get(0);
            // 删除 series 数组中的所有对象
            series.removeAll();

            // 根据 selectedXAxisNames.size() 进行复制第一个对象，并替换 name 和 datasetIndex 的值
            for (int i = 0; i < legendNameList.size(); i++) {
                // 复制第一个对象
                JsonNode newSeries = firstSeries.deepCopy();

                // 替换 name 的值
                ((ObjectNode) newSeries).put("name", legendNameList.get(i));

                // 设置 datasetIndex 的值
                ((ObjectNode) newSeries).put("datasetIndex", i);

                // 将新对象添加到 series 数组中
                series.add(newSeries);
            }

            //处理dataset的fromDatasetIndex
            // 处理 dataset 的 fromDatasetIndex
            ArrayNode datasetArray = (ArrayNode) rootNode.path("dataset");
            // 查找并保留第一个含有 fromDatasetIndex 的对象，删除其他含有 fromDatasetIndex 的对象
            JsonNode firstFromDatasetIndexNode = null;
            int firstFromDatasetIndexNodeIndex = -1;
            for (int i = 0; i < datasetArray.size(); i++) {
                JsonNode node = datasetArray.get(i);
                if (node.has("fromDatasetIndex")) {
                    if (firstFromDatasetIndexNode == null) {
                        // 记录第一个含有 fromDatasetIndex 的对象，并保存其索引
                        firstFromDatasetIndexNode = node;
                        firstFromDatasetIndexNodeIndex = i;
                    } else {
                        // 删除其他含有 fromDatasetIndex 的对象
                        datasetArray.remove(i);
                        i--; // 调整索引，因为我们删除了一个元素
                    }
                }
            }

            // 根据 lenSize 的值复制 firstFromDatasetIndexNode，并添加到 dataset 中
            // 假设 integerValue 已经被正确赋值
            int insertIndexa = firstFromDatasetIndexNodeIndex + 1; // 插入位置在第一个含有 fromDatasetIndex 的对象之后
            for (int i = 1; i < lenSize; i++) { // 从1开始，因为第一个已经存在
                JsonNode newFromDatasetIndexNode = firstFromDatasetIndexNode.deepCopy();
                ((ObjectNode) newFromDatasetIndexNode).put("fromDatasetIndex", i); // 设置 fromDatasetIndex 的值
                datasetArray.insert(insertIndexa, newFromDatasetIndexNode);
                insertIndexa++; // 每次插入后，更新插入位置
            }


// 获取 dataset 中含有 fromDatasetIndex 的最后一个值
            int maxFromDatasetIndex = -1;
            for (JsonNode node : dataset) {
                if (node.has("fromDatasetIndex")) {
                    int fromDatasetIndex = node.get("fromDatasetIndex").asInt();
                    if (fromDatasetIndex > maxFromDatasetIndex) {
                        maxFromDatasetIndex = fromDatasetIndex;
                    }
                }
            }

            // 更新 series 数组中的 datasetIndex 从 maxFromDatasetIndex 递增
            if (maxFromDatasetIndex != -1) {
                ArrayNode seriesa = (ArrayNode) rootNode.path("series");
                int datasetIndex = maxFromDatasetIndex + 1; // 从最大值开始递增

                for (JsonNode seriesNode : seriesa) {
                    ((ObjectNode) seriesNode).put("datasetIndex", datasetIndex);
                    datasetIndex++; // 递增 datasetIndex
                }
            }


            // 打印完整的 JSON 数据
            String updatedJson = rootNode.toString();
            ArrayList<Object> newList = new ArrayList<>();
            ChartSmartVo chartSmartVo = new ChartSmartVo();
            chartSmartVo.setConfigValue(updatedJson);
            chartSmartVo.setUniList(selectedXAxisNames);
            newList.add(chartSmartVo);

            return AjaxResult.success(newList);
        } catch (JsonProcessingException e) {
            log.error("importData4接口的configId:{}发送异常:{}", chartDateTto.getConfigId(), e.getMessage());
            throw new RuntimeException(e);
        }
    }


}

